blob: 61c0c4e9088561d5fe833678bce1d8362d24b801 [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"
Jamie Madilld4a3a312014-06-25 16:04:56 -04008#include "compiler/translator/Compiler.h"
Geoff Lang17732822013-08-29 13:46:49 -04009#include "compiler/translator/DetectCallDepth.h"
10#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"
Geoff Lang17732822013-08-29 13:46:49 -040015#include "compiler/translator/RenameFunction.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
Jamie Madill68fe74a2014-05-27 12:56:01 -040095TCompiler::TCompiler(ShShaderType type, ShShaderSpec spec, ShShaderOutput output)
alokp@chromium.org4888ceb2010-10-01 21:13:12 +000096 : shaderType(type),
zmo@google.comf420c422011-09-12 18:27:59 +000097 shaderSpec(spec),
Jamie Madill68fe74a2014-05-27 12:56:01 -040098 outputType(output),
Jamie Madilleb1a0102013-07-08 13:31:38 -040099 maxUniformVectors(0),
100 maxExpressionComplexity(0),
101 maxCallStackDepth(0),
shannon.woods%transgaming.com@gtempaccount.comcbb6b6a2013-04-13 03:27:47 +0000102 fragmentPrecisionHigh(false),
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000103 clampingStrategy(SH_CLAMP_WITH_CLAMP_INTRINSIC),
zmo@google.com9996b8e2012-01-19 01:43:55 +0000104 builtInFunctionEmulator(type)
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000105{
106}
107
108TCompiler::~TCompiler()
109{
110}
111
112bool TCompiler::Init(const ShBuiltInResources& resources)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000113{
shannon.woods%transgaming.com@gtempaccount.com0bbed382013-04-13 03:38:07 +0000114 shaderVersion = 100;
gman@chromium.org8d804792012-10-17 21:33:48 +0000115 maxUniformVectors = (shaderType == SH_VERTEX_SHADER) ?
116 resources.MaxVertexUniformVectors :
117 resources.MaxFragmentUniformVectors;
Jamie Madilleb1a0102013-07-08 13:31:38 -0400118 maxExpressionComplexity = resources.MaxExpressionComplexity;
119 maxCallStackDepth = resources.MaxCallStackDepth;
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400120
121 SetGlobalPoolAllocator(&allocator);
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000122
alokp@chromium.org07620a52010-09-23 17:53:56 +0000123 // Generate built-in symbol table.
124 if (!InitBuiltInSymbolTable(resources))
125 return false;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000126 InitExtensionBehavior(resources, extensionBehavior);
shannon.woods%transgaming.com@gtempaccount.comcbb6b6a2013-04-13 03:27:47 +0000127 fragmentPrecisionHigh = resources.FragmentPrecisionHigh == 1;
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000128
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000129 arrayBoundsClamper.SetClampingStrategy(resources.ArrayIndexClampingStrategy);
130 clampingStrategy = resources.ArrayIndexClampingStrategy;
131
daniel@transgaming.comc23f4612012-11-28 19:42:57 +0000132 hashFunction = resources.HashFunction;
133
alokp@chromium.org07620a52010-09-23 17:53:56 +0000134 return true;
135}
136
137bool TCompiler::compile(const char* const shaderStrings[],
shannon.woods@transgaming.comd64b3da2013-02-28 23:19:26 +0000138 size_t numStrings,
alokp@chromium.org07620a52010-09-23 17:53:56 +0000139 int compileOptions)
140{
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400141 TScopedPoolAllocator scopedAlloc(&allocator);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000142 clearResults();
143
144 if (numStrings == 0)
145 return true;
146
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000147 // If compiling for WebGL, validate loop and indexing as well.
Jamie Madill5508f392014-02-20 13:31:36 -0500148 if (IsWebGLBasedSpec(shaderSpec))
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000149 compileOptions |= SH_VALIDATE_LOOP_INDEXING;
alokp@chromium.org1f299542010-11-12 15:50:23 +0000150
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000151 // First string is path of source file if flag is set. The actual source follows.
152 const char* sourcePath = NULL;
shannon.woods@transgaming.comd64b3da2013-02-28 23:19:26 +0000153 size_t firstSource = 0;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000154 if (compileOptions & SH_SOURCE_PATH)
155 {
156 sourcePath = shaderStrings[0];
157 ++firstSource;
158 }
159
alokp@chromium.org07620a52010-09-23 17:53:56 +0000160 TIntermediate intermediate(infoSink);
161 TParseContext parseContext(symbolTable, extensionBehavior, intermediate,
zmo@google.comdc4b4f82011-06-17 00:42:53 +0000162 shaderType, shaderSpec, compileOptions, true,
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000163 sourcePath, infoSink);
shannon.woods%transgaming.com@gtempaccount.comcbb6b6a2013-04-13 03:27:47 +0000164 parseContext.fragmentPrecisionHigh = fragmentPrecisionHigh;
Alok Priyadarshi8156b6b2013-09-23 14:56:58 -0400165 SetGlobalParseContext(&parseContext);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000166
167 // We preserve symbols at the built-in level from compile-to-compile.
168 // Start pushing the user-defined symbols at global level.
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400169 TScopedSymbolTableLevel scopedSymbolLevel(&symbolTable);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000170
171 // Parse shader.
172 bool success =
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000173 (PaParseStrings(numStrings - firstSource, &shaderStrings[firstSource], NULL, &parseContext) == 0) &&
alokp@chromium.org07620a52010-09-23 17:53:56 +0000174 (parseContext.treeRoot != NULL);
shannon.woods%transgaming.com@gtempaccount.com0bbed382013-04-13 03:38:07 +0000175
shannon.woods%transgaming.com@gtempaccount.com5524db02013-04-13 03:38:16 +0000176 shaderVersion = parseContext.getShaderVersion();
shannon.woods%transgaming.com@gtempaccount.com0bbed382013-04-13 03:38:07 +0000177
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800178 if (success)
179 {
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000180 TIntermNode* root = parseContext.treeRoot;
181 success = intermediate.postProcess(root);
182
Jamie Madill6654bc92014-03-26 14:01:57 -0400183 // Disallow expressions deemed too complex.
184 if (success && (compileOptions & SH_LIMIT_EXPRESSION_COMPLEXITY))
185 success = limitExpressionComplexity(root);
186
zmo@google.comb1762df2011-07-30 02:04:23 +0000187 if (success)
Jamie Madilleb1a0102013-07-08 13:31:38 -0400188 success = detectCallDepth(root, infoSink, (compileOptions & SH_LIMIT_CALL_STACK_DEPTH) != 0);
zmo@google.comb1762df2011-07-30 02:04:23 +0000189
Jamie Madill05a80ce2013-06-20 11:55:49 -0400190 if (success && shaderVersion == 300 && shaderType == SH_FRAGMENT_SHADER)
191 success = validateOutputs(root);
192
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000193 if (success && (compileOptions & SH_VALIDATE_LOOP_INDEXING))
194 success = validateLimitations(root);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000195
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000196 if (success && (compileOptions & SH_TIMING_RESTRICTIONS))
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000197 success = enforceTimingRestrictions(root, (compileOptions & SH_DEPENDENCY_GRAPH) != 0);
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000198
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +0000199 if (success && shaderSpec == SH_CSS_SHADERS_SPEC)
200 rewriteCSSShader(root);
201
zmo@google.com0c6bb7a2011-08-17 19:39:58 +0000202 // Unroll for-loop markup needs to happen after validateLimitations pass.
203 if (success && (compileOptions & SH_UNROLL_FOR_LOOP_WITH_INTEGER_INDEX))
Zhenyao Mo3cdfcce2014-03-07 13:00:08 -0800204 {
205 ForLoopUnrollMarker marker(ForLoopUnrollMarker::kIntegerIndex);
Zhenyao Mo550c6002014-02-26 15:40:48 -0800206 root->traverse(&marker);
207 }
208 if (success && (compileOptions & SH_UNROLL_FOR_LOOP_WITH_SAMPLER_ARRAY_INDEX))
Zhenyao Mo3cdfcce2014-03-07 13:00:08 -0800209 {
210 ForLoopUnrollMarker marker(ForLoopUnrollMarker::kSamplerArrayIndex);
Zhenyao Mo550c6002014-02-26 15:40:48 -0800211 root->traverse(&marker);
212 if (marker.samplerArrayIndexIsFloatLoopIndex())
213 {
214 infoSink.info.prefix(EPrefixError);
215 infoSink.info << "sampler array index is float loop index";
216 success = false;
217 }
218 }
zmo@google.com0c6bb7a2011-08-17 19:39:58 +0000219
zmo@google.com32e97312011-08-24 01:03:11 +0000220 // Built-in function emulation needs to happen after validateLimitations pass.
221 if (success && (compileOptions & SH_EMULATE_BUILT_IN_FUNCTIONS))
222 builtInFunctionEmulator.MarkBuiltInFunctionsForEmulation(root);
223
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000224 // Clamping uniform array bounds needs to happen after validateLimitations pass.
225 if (success && (compileOptions & SH_CLAMP_INDIRECT_ARRAY_BOUNDS))
226 arrayBoundsClamper.MarkIndirectArrayBoundsForClamping(root);
227
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800228 if (success && shaderType == SH_VERTEX_SHADER && (compileOptions & SH_INIT_GL_POSITION))
229 initializeGLPosition(root);
Zhenyao Moac44cd22013-09-23 14:57:09 -0400230
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800231 if (success && (compileOptions & SH_UNFOLD_SHORT_CIRCUIT))
232 {
Zhenyao Mo7cab38b2013-10-15 12:59:30 -0700233 UnfoldShortCircuitAST unfoldShortCircuit;
234 root->traverse(&unfoldShortCircuit);
235 unfoldShortCircuit.updateTree();
236 }
237
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800238 if (success && (compileOptions & SH_VARIABLES))
239 {
Zhenyao Mo74da9f22013-09-23 14:57:01 -0400240 collectVariables(root);
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800241 if (compileOptions & SH_ENFORCE_PACKING_RESTRICTIONS)
242 {
gman@chromium.org8d804792012-10-17 21:33:48 +0000243 success = enforcePackingRestrictions();
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800244 if (!success)
245 {
Jamie Madill075edd82013-07-08 13:30:19 -0400246 infoSink.info.prefix(EPrefixError);
247 infoSink.info << "too many uniforms";
gman@chromium.org8d804792012-10-17 21:33:48 +0000248 }
249 }
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800250 if (success && shaderType == SH_VERTEX_SHADER &&
251 (compileOptions & SH_INIT_VARYINGS_WITHOUT_STATIC_USE))
252 initializeVaryingsWithoutStaticUse(root);
gman@chromium.org8d804792012-10-17 21:33:48 +0000253 }
zmo@google.comfd747b82011-04-23 01:30:07 +0000254
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000255 if (success && (compileOptions & SH_INTERMEDIATE_TREE))
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000256 intermediate.outputTree(root);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000257
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000258 if (success && (compileOptions & SH_OBJECT_CODE))
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000259 translate(root);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000260 }
261
262 // Cleanup memory.
263 intermediate.remove(parseContext.treeRoot);
Zhenyao Mo7faf1a12014-04-25 18:03:56 -0700264 SetGlobalParseContext(NULL);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000265 return success;
266}
267
Nicolas Capens49a88872013-06-20 09:54:03 -0400268bool TCompiler::InitBuiltInSymbolTable(const ShBuiltInResources &resources)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000269{
shannon.woods%transgaming.com@gtempaccount.com18b4c4b2013-04-13 03:31:40 +0000270 compileResources = resources;
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400271 setResourceString();
shannonwoods@chromium.org2ac0be92013-05-30 00:02:27 +0000272
Nicolas Capens49a88872013-06-20 09:54:03 -0400273 assert(symbolTable.isEmpty());
274 symbolTable.push(); // COMMON_BUILTINS
275 symbolTable.push(); // ESSL1_BUILTINS
276 symbolTable.push(); // ESSL3_BUILTINS
shannonwoods@chromium.org2ac0be92013-05-30 00:02:27 +0000277
Nicolas Capens49a88872013-06-20 09:54:03 -0400278 TPublicType integer;
279 integer.type = EbtInt;
280 integer.primarySize = 1;
281 integer.secondarySize = 1;
282 integer.array = false;
283
284 TPublicType floatingPoint;
285 floatingPoint.type = EbtFloat;
286 floatingPoint.primarySize = 1;
287 floatingPoint.secondarySize = 1;
288 floatingPoint.array = false;
289
Zhenyao Moa5a1dfc2013-09-23 14:57:03 -0400290 TPublicType sampler;
291 sampler.primarySize = 1;
292 sampler.secondarySize = 1;
293 sampler.array = false;
294
Nicolas Capens49a88872013-06-20 09:54:03 -0400295 switch(shaderType)
296 {
297 case SH_FRAGMENT_SHADER:
298 symbolTable.setDefaultPrecision(integer, EbpMedium);
299 break;
300 case SH_VERTEX_SHADER:
301 symbolTable.setDefaultPrecision(integer, EbpHigh);
302 symbolTable.setDefaultPrecision(floatingPoint, EbpHigh);
303 break;
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800304 default:
305 assert(false && "Language not supported");
Nicolas Capens49a88872013-06-20 09:54:03 -0400306 }
Zhenyao Moa5a1dfc2013-09-23 14:57:03 -0400307 // We set defaults for all the sampler types, even those that are
308 // only available if an extension exists.
309 for (int samplerType = EbtGuardSamplerBegin + 1;
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800310 samplerType < EbtGuardSamplerEnd; ++samplerType)
311 {
Zhenyao Moa5a1dfc2013-09-23 14:57:03 -0400312 sampler.type = static_cast<TBasicType>(samplerType);
313 symbolTable.setDefaultPrecision(sampler, EbpLow);
314 }
Nicolas Capens49a88872013-06-20 09:54:03 -0400315
Jamie Madill1b452142013-07-12 14:51:11 -0400316 InsertBuiltInFunctions(shaderType, shaderSpec, resources, symbolTable);
Nicolas Capens49a88872013-06-20 09:54:03 -0400317
318 IdentifyBuiltIns(shaderType, shaderSpec, resources, symbolTable);
319
320 return true;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000321}
322
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400323void TCompiler::setResourceString()
324{
325 std::ostringstream strstream;
326 strstream << ":MaxVertexAttribs:" << compileResources.MaxVertexAttribs
327 << ":MaxVertexUniformVectors:" << compileResources.MaxVertexUniformVectors
328 << ":MaxVaryingVectors:" << compileResources.MaxVaryingVectors
329 << ":MaxVertexTextureImageUnits:" << compileResources.MaxVertexTextureImageUnits
330 << ":MaxCombinedTextureImageUnits:" << compileResources.MaxCombinedTextureImageUnits
331 << ":MaxTextureImageUnits:" << compileResources.MaxTextureImageUnits
332 << ":MaxFragmentUniformVectors:" << compileResources.MaxFragmentUniformVectors
333 << ":MaxDrawBuffers:" << compileResources.MaxDrawBuffers
334 << ":OES_standard_derivatives:" << compileResources.OES_standard_derivatives
335 << ":OES_EGL_image_external:" << compileResources.OES_EGL_image_external
336 << ":ARB_texture_rectangle:" << compileResources.ARB_texture_rectangle
337 << ":EXT_draw_buffers:" << compileResources.EXT_draw_buffers
338 << ":FragmentPrecisionHigh:" << compileResources.FragmentPrecisionHigh
339 << ":MaxExpressionComplexity:" << compileResources.MaxExpressionComplexity
340 << ":MaxCallStackDepth:" << compileResources.MaxCallStackDepth
341 << ":EXT_frag_depth:" << compileResources.EXT_frag_depth
342 << ":EXT_shader_texture_lod:" << compileResources.EXT_shader_texture_lod
343 << ":MaxVertexOutputVectors:" << compileResources.MaxVertexOutputVectors
344 << ":MaxFragmentInputVectors:" << compileResources.MaxFragmentInputVectors
345 << ":MinProgramTexelOffset:" << compileResources.MinProgramTexelOffset
346 << ":MaxProgramTexelOffset:" << compileResources.MaxProgramTexelOffset;
347
348 builtInResourcesString = strstream.str();
349}
350
alokp@chromium.org07620a52010-09-23 17:53:56 +0000351void TCompiler::clearResults()
352{
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000353 arrayBoundsClamper.Cleanup();
alokp@chromium.org07620a52010-09-23 17:53:56 +0000354 infoSink.info.erase();
355 infoSink.obj.erase();
356 infoSink.debug.erase();
357
358 attribs.clear();
359 uniforms.clear();
Zhenyao Mod2d340b2013-09-23 14:57:05 -0400360 varyings.clear();
zmo@google.coma3b4ab42011-09-16 00:53:26 +0000361
362 builtInFunctionEmulator.Cleanup();
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +0000363
364 nameMap.clear();
alokp@chromium.org07620a52010-09-23 17:53:56 +0000365}
366
Jamie Madilleb1a0102013-07-08 13:31:38 -0400367bool TCompiler::detectCallDepth(TIntermNode* root, TInfoSink& infoSink, bool limitCallStackDepth)
zmo@google.comb1762df2011-07-30 02:04:23 +0000368{
Jamie Madilleb1a0102013-07-08 13:31:38 -0400369 DetectCallDepth detect(infoSink, limitCallStackDepth, maxCallStackDepth);
zmo@google.comb1762df2011-07-30 02:04:23 +0000370 root->traverse(&detect);
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800371 switch (detect.detectCallDepth())
372 {
373 case DetectCallDepth::kErrorNone:
374 return true;
375 case DetectCallDepth::kErrorMissingMain:
376 infoSink.info.prefix(EPrefixError);
377 infoSink.info << "Missing main()";
378 return false;
379 case DetectCallDepth::kErrorRecursion:
380 infoSink.info.prefix(EPrefixError);
381 infoSink.info << "Function recursion detected";
382 return false;
383 case DetectCallDepth::kErrorMaxDepthExceeded:
384 infoSink.info.prefix(EPrefixError);
385 infoSink.info << "Function call stack too deep";
386 return false;
387 default:
388 UNREACHABLE();
389 return false;
zmo@google.comb1762df2011-07-30 02:04:23 +0000390 }
391}
392
Jamie Madill05a80ce2013-06-20 11:55:49 -0400393bool TCompiler::validateOutputs(TIntermNode* root)
394{
395 ValidateOutputs validateOutputs(infoSink.info, compileResources.MaxDrawBuffers);
396 root->traverse(&validateOutputs);
397 return (validateOutputs.numErrors() == 0);
398}
399
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +0000400void TCompiler::rewriteCSSShader(TIntermNode* root)
401{
402 RenameFunction renamer("main(", "css_main(");
403 root->traverse(&renamer);
404}
405
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800406bool TCompiler::validateLimitations(TIntermNode* root)
407{
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000408 ValidateLimitations validate(shaderType, infoSink.info);
409 root->traverse(&validate);
410 return validate.numErrors() == 0;
411}
412
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000413bool TCompiler::enforceTimingRestrictions(TIntermNode* root, bool outputGraph)
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000414{
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800415 if (shaderSpec != SH_WEBGL_SPEC)
416 {
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000417 infoSink.info << "Timing restrictions must be enforced under the WebGL spec.";
418 return false;
419 }
420
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800421 if (shaderType == SH_FRAGMENT_SHADER)
422 {
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000423 TDependencyGraph graph(root);
424
425 // Output any errors first.
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000426 bool success = enforceFragmentShaderTimingRestrictions(graph);
Jamie Madill5508f392014-02-20 13:31:36 -0500427
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000428 // Then, output the dependency graph.
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800429 if (outputGraph)
430 {
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000431 TDependencyGraphOutput output(infoSink.info);
432 output.outputAllSpanningTrees(graph);
433 }
Jamie Madill5508f392014-02-20 13:31:36 -0500434
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000435 return success;
436 }
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800437 else
438 {
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000439 return enforceVertexShaderTimingRestrictions(root);
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000440 }
441}
442
Jamie Madilleb1a0102013-07-08 13:31:38 -0400443bool TCompiler::limitExpressionComplexity(TIntermNode* root)
444{
Jamie Madill6654bc92014-03-26 14:01:57 -0400445 TMaxDepthTraverser traverser(maxExpressionComplexity+1);
Jamie Madilleb1a0102013-07-08 13:31:38 -0400446 root->traverse(&traverser);
Jamie Madill6654bc92014-03-26 14:01:57 -0400447
448 if (traverser.getMaxDepth() > maxExpressionComplexity)
449 {
450 infoSink.info << "Expression too complex.";
451 return false;
452 }
453
Jamie Madilleb1a0102013-07-08 13:31:38 -0400454 TDependencyGraph graph(root);
455
456 for (TFunctionCallVector::const_iterator iter = graph.beginUserDefinedFunctionCalls();
457 iter != graph.endUserDefinedFunctionCalls();
458 ++iter)
459 {
460 TGraphFunctionCall* samplerSymbol = *iter;
461 TDependencyGraphTraverser graphTraverser;
462 samplerSymbol->traverse(&graphTraverser);
463 }
464
Jamie Madilleb1a0102013-07-08 13:31:38 -0400465 return true;
466}
467
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000468bool TCompiler::enforceFragmentShaderTimingRestrictions(const TDependencyGraph& graph)
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000469{
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000470 RestrictFragmentShaderTiming restrictor(infoSink.info);
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000471 restrictor.enforceRestrictions(graph);
472 return restrictor.numErrors() == 0;
473}
474
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000475bool TCompiler::enforceVertexShaderTimingRestrictions(TIntermNode* root)
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000476{
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000477 RestrictVertexShaderTiming restrictor(infoSink.info);
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000478 restrictor.enforceRestrictions(root);
479 return restrictor.numErrors() == 0;
480}
481
Zhenyao Mo74da9f22013-09-23 14:57:01 -0400482void TCompiler::collectVariables(TIntermNode* root)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000483{
Zhenyao Mo74da9f22013-09-23 14:57:01 -0400484 CollectVariables collect(attribs, uniforms, varyings, hashFunction);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000485 root->traverse(&collect);
486}
zmo@google.comfd747b82011-04-23 01:30:07 +0000487
gman@chromium.org8d804792012-10-17 21:33:48 +0000488bool TCompiler::enforcePackingRestrictions()
489{
490 VariablePacker packer;
491 return packer.CheckVariablesWithinPackingLimits(maxUniformVectors, uniforms);
492}
493
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800494void TCompiler::initializeGLPosition(TIntermNode* root)
495{
496 InitializeVariables::InitVariableInfoList variables;
497 InitializeVariables::InitVariableInfo var(
498 "gl_Position", TType(EbtFloat, EbpUndefined, EvqPosition, 4));
499 variables.push_back(var);
500 InitializeVariables initializer(variables);
501 root->traverse(&initializer);
502}
503
504void TCompiler::initializeVaryingsWithoutStaticUse(TIntermNode* root)
505{
506 InitializeVariables::InitVariableInfoList variables;
507 for (size_t ii = 0; ii < varyings.size(); ++ii)
508 {
509 const TVariableInfo& varying = varyings[ii];
510 if (varying.staticUse)
511 continue;
Zhenyao Mo66e5dda2014-04-28 11:13:25 -0700512 unsigned char primarySize = 1, secondarySize = 1;
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800513 switch (varying.type)
514 {
515 case SH_FLOAT:
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800516 break;
517 case SH_FLOAT_VEC2:
Zhenyao Mo66e5dda2014-04-28 11:13:25 -0700518 primarySize = 2;
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800519 break;
520 case SH_FLOAT_VEC3:
Zhenyao Mo66e5dda2014-04-28 11:13:25 -0700521 primarySize = 3;
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800522 break;
523 case SH_FLOAT_VEC4:
Zhenyao Mo66e5dda2014-04-28 11:13:25 -0700524 primarySize = 4;
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800525 break;
526 case SH_FLOAT_MAT2:
Zhenyao Mo66e5dda2014-04-28 11:13:25 -0700527 primarySize = 2;
528 secondarySize = 2;
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800529 break;
530 case SH_FLOAT_MAT3:
Zhenyao Mo66e5dda2014-04-28 11:13:25 -0700531 primarySize = 3;
532 secondarySize = 3;
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800533 break;
534 case SH_FLOAT_MAT4:
Zhenyao Mo66e5dda2014-04-28 11:13:25 -0700535 primarySize = 4;
536 secondarySize = 4;
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800537 break;
538 default:
539 ASSERT(false);
540 }
Zhenyao Mo66e5dda2014-04-28 11:13:25 -0700541 TType type(EbtFloat, EbpUndefined, EvqVaryingOut, primarySize, secondarySize, varying.isArray);
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800542 TString name = varying.name.c_str();
543 if (varying.isArray)
544 {
545 type.setArraySize(varying.size);
546 name = name.substr(0, name.find_first_of('['));
547 }
548
549 InitializeVariables::InitVariableInfo var(name, type);
550 variables.push_back(var);
551 }
552 InitializeVariables initializer(variables);
553 root->traverse(&initializer);
554}
555
zmo@google.com5601ea02011-06-10 18:23:25 +0000556const TExtensionBehavior& TCompiler::getExtensionBehavior() const
557{
558 return extensionBehavior;
559}
zmo@google.com32e97312011-08-24 01:03:11 +0000560
shannon.woods%transgaming.com@gtempaccount.com18b4c4b2013-04-13 03:31:40 +0000561const ShBuiltInResources& TCompiler::getResources() const
562{
563 return compileResources;
564}
565
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000566const ArrayBoundsClamper& TCompiler::getArrayBoundsClamper() const
567{
568 return arrayBoundsClamper;
569}
570
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000571ShArrayIndexClampingStrategy TCompiler::getArrayIndexClampingStrategy() const
572{
573 return clampingStrategy;
574}
575
576const BuiltInFunctionEmulator& TCompiler::getBuiltInFunctionEmulator() const
577{
578 return builtInFunctionEmulator;
579}