blob: 898e3efd4e64ac563510269dc45513263f6e676e [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"
Jamie Madill183bde52014-07-02 15:31:19 -040025#include "angle_gl.h"
alokp@chromium.org07620a52010-09-23 17:53:56 +000026
Jamie Madill5508f392014-02-20 13:31:36 -050027bool IsWebGLBasedSpec(ShShaderSpec spec)
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +000028{
29 return spec == SH_WEBGL_SPEC || spec == SH_CSS_SHADERS_SPEC;
30}
31
Zhenyao Mo7faf1a12014-04-25 18:03:56 -070032size_t GetGlobalMaxTokenSize(ShShaderSpec spec)
Jamie Madill88f6e942014-02-19 10:27:53 -050033{
Jamie Madill88f6e942014-02-19 10:27:53 -050034 // WebGL defines a max token legnth of 256, while ES2 leaves max token
35 // size undefined. ES3 defines a max size of 1024 characters.
Zhenyao Mo7faf1a12014-04-25 18:03:56 -070036 if (IsWebGLBasedSpec(spec))
Jamie Madill88f6e942014-02-19 10:27:53 -050037 {
38 return 256;
39 }
40 else
41 {
42 return 1024;
43 }
44}
45
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +000046namespace {
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080047class TScopedPoolAllocator
48{
49 public:
50 TScopedPoolAllocator(TPoolAllocator* allocator) : mAllocator(allocator)
51 {
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -040052 mAllocator->push();
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +000053 SetGlobalPoolAllocator(mAllocator);
54 }
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080055 ~TScopedPoolAllocator()
56 {
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +000057 SetGlobalPoolAllocator(NULL);
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -040058 mAllocator->pop();
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +000059 }
60
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080061 private:
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +000062 TPoolAllocator* mAllocator;
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -040063};
64
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080065class TScopedSymbolTableLevel
66{
67 public:
68 TScopedSymbolTableLevel(TSymbolTable* table) : mTable(table)
69 {
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -040070 ASSERT(mTable->atBuiltInLevel());
71 mTable->push();
72 }
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080073 ~TScopedSymbolTableLevel()
74 {
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -040075 while (!mTable->atBuiltInLevel())
76 mTable->pop();
77 }
78
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080079 private:
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -040080 TSymbolTable* mTable;
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +000081};
82} // namespace
83
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080084TShHandleBase::TShHandleBase()
85{
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +000086 allocator.push();
87 SetGlobalPoolAllocator(&allocator);
88}
89
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080090TShHandleBase::~TShHandleBase()
91{
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +000092 SetGlobalPoolAllocator(NULL);
93 allocator.popAll();
94}
95
Jamie Madill183bde52014-07-02 15:31:19 -040096TCompiler::TCompiler(sh::GLenum type, ShShaderSpec spec, ShShaderOutput output)
alokp@chromium.org4888ceb2010-10-01 21:13:12 +000097 : shaderType(type),
zmo@google.comf420c422011-09-12 18:27:59 +000098 shaderSpec(spec),
Jamie Madill68fe74a2014-05-27 12:56:01 -040099 outputType(output),
Jamie Madilleb1a0102013-07-08 13:31:38 -0400100 maxUniformVectors(0),
101 maxExpressionComplexity(0),
102 maxCallStackDepth(0),
shannon.woods%transgaming.com@gtempaccount.comcbb6b6a2013-04-13 03:27:47 +0000103 fragmentPrecisionHigh(false),
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000104 clampingStrategy(SH_CLAMP_WITH_CLAMP_INTRINSIC),
zmo@google.com9996b8e2012-01-19 01:43:55 +0000105 builtInFunctionEmulator(type)
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000106{
107}
108
109TCompiler::~TCompiler()
110{
111}
112
113bool TCompiler::Init(const ShBuiltInResources& resources)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000114{
shannon.woods%transgaming.com@gtempaccount.com0bbed382013-04-13 03:38:07 +0000115 shaderVersion = 100;
Jamie Madill183bde52014-07-02 15:31:19 -0400116 maxUniformVectors = (shaderType == GL_VERTEX_SHADER) ?
gman@chromium.org8d804792012-10-17 21:33:48 +0000117 resources.MaxVertexUniformVectors :
118 resources.MaxFragmentUniformVectors;
Jamie Madilleb1a0102013-07-08 13:31:38 -0400119 maxExpressionComplexity = resources.MaxExpressionComplexity;
120 maxCallStackDepth = resources.MaxCallStackDepth;
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400121
122 SetGlobalPoolAllocator(&allocator);
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000123
alokp@chromium.org07620a52010-09-23 17:53:56 +0000124 // Generate built-in symbol table.
125 if (!InitBuiltInSymbolTable(resources))
126 return false;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000127 InitExtensionBehavior(resources, extensionBehavior);
shannon.woods%transgaming.com@gtempaccount.comcbb6b6a2013-04-13 03:27:47 +0000128 fragmentPrecisionHigh = resources.FragmentPrecisionHigh == 1;
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000129
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000130 arrayBoundsClamper.SetClampingStrategy(resources.ArrayIndexClampingStrategy);
131 clampingStrategy = resources.ArrayIndexClampingStrategy;
132
daniel@transgaming.comc23f4612012-11-28 19:42:57 +0000133 hashFunction = resources.HashFunction;
134
alokp@chromium.org07620a52010-09-23 17:53:56 +0000135 return true;
136}
137
138bool TCompiler::compile(const char* const shaderStrings[],
shannon.woods@transgaming.comd64b3da2013-02-28 23:19:26 +0000139 size_t numStrings,
alokp@chromium.org07620a52010-09-23 17:53:56 +0000140 int compileOptions)
141{
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400142 TScopedPoolAllocator scopedAlloc(&allocator);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000143 clearResults();
144
145 if (numStrings == 0)
146 return true;
147
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000148 // If compiling for WebGL, validate loop and indexing as well.
Jamie Madill5508f392014-02-20 13:31:36 -0500149 if (IsWebGLBasedSpec(shaderSpec))
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000150 compileOptions |= SH_VALIDATE_LOOP_INDEXING;
alokp@chromium.org1f299542010-11-12 15:50:23 +0000151
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000152 // First string is path of source file if flag is set. The actual source follows.
153 const char* sourcePath = NULL;
shannon.woods@transgaming.comd64b3da2013-02-28 23:19:26 +0000154 size_t firstSource = 0;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000155 if (compileOptions & SH_SOURCE_PATH)
156 {
157 sourcePath = shaderStrings[0];
158 ++firstSource;
159 }
160
alokp@chromium.org07620a52010-09-23 17:53:56 +0000161 TIntermediate intermediate(infoSink);
162 TParseContext parseContext(symbolTable, extensionBehavior, intermediate,
zmo@google.comdc4b4f82011-06-17 00:42:53 +0000163 shaderType, shaderSpec, compileOptions, true,
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000164 sourcePath, infoSink);
shannon.woods%transgaming.com@gtempaccount.comcbb6b6a2013-04-13 03:27:47 +0000165 parseContext.fragmentPrecisionHigh = fragmentPrecisionHigh;
Alok Priyadarshi8156b6b2013-09-23 14:56:58 -0400166 SetGlobalParseContext(&parseContext);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000167
168 // We preserve symbols at the built-in level from compile-to-compile.
169 // Start pushing the user-defined symbols at global level.
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400170 TScopedSymbolTableLevel scopedSymbolLevel(&symbolTable);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000171
172 // Parse shader.
173 bool success =
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000174 (PaParseStrings(numStrings - firstSource, &shaderStrings[firstSource], NULL, &parseContext) == 0) &&
alokp@chromium.org07620a52010-09-23 17:53:56 +0000175 (parseContext.treeRoot != NULL);
shannon.woods%transgaming.com@gtempaccount.com0bbed382013-04-13 03:38:07 +0000176
shannon.woods%transgaming.com@gtempaccount.com5524db02013-04-13 03:38:16 +0000177 shaderVersion = parseContext.getShaderVersion();
shannon.woods%transgaming.com@gtempaccount.com0bbed382013-04-13 03:38:07 +0000178
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800179 if (success)
180 {
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000181 TIntermNode* root = parseContext.treeRoot;
182 success = intermediate.postProcess(root);
183
Jamie Madill6654bc92014-03-26 14:01:57 -0400184 // Disallow expressions deemed too complex.
185 if (success && (compileOptions & SH_LIMIT_EXPRESSION_COMPLEXITY))
186 success = limitExpressionComplexity(root);
187
zmo@google.comb1762df2011-07-30 02:04:23 +0000188 if (success)
Jamie Madilleb1a0102013-07-08 13:31:38 -0400189 success = detectCallDepth(root, infoSink, (compileOptions & SH_LIMIT_CALL_STACK_DEPTH) != 0);
zmo@google.comb1762df2011-07-30 02:04:23 +0000190
Jamie Madill183bde52014-07-02 15:31:19 -0400191 if (success && shaderVersion == 300 && shaderType == GL_FRAGMENT_SHADER)
Jamie Madill05a80ce2013-06-20 11:55:49 -0400192 success = validateOutputs(root);
193
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000194 if (success && (compileOptions & SH_VALIDATE_LOOP_INDEXING))
195 success = validateLimitations(root);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000196
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000197 if (success && (compileOptions & SH_TIMING_RESTRICTIONS))
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000198 success = enforceTimingRestrictions(root, (compileOptions & SH_DEPENDENCY_GRAPH) != 0);
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000199
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +0000200 if (success && shaderSpec == SH_CSS_SHADERS_SPEC)
201 rewriteCSSShader(root);
202
zmo@google.com0c6bb7a2011-08-17 19:39:58 +0000203 // Unroll for-loop markup needs to happen after validateLimitations pass.
204 if (success && (compileOptions & SH_UNROLL_FOR_LOOP_WITH_INTEGER_INDEX))
Zhenyao Mo3cdfcce2014-03-07 13:00:08 -0800205 {
206 ForLoopUnrollMarker marker(ForLoopUnrollMarker::kIntegerIndex);
Zhenyao Mo550c6002014-02-26 15:40:48 -0800207 root->traverse(&marker);
208 }
209 if (success && (compileOptions & SH_UNROLL_FOR_LOOP_WITH_SAMPLER_ARRAY_INDEX))
Zhenyao Mo3cdfcce2014-03-07 13:00:08 -0800210 {
211 ForLoopUnrollMarker marker(ForLoopUnrollMarker::kSamplerArrayIndex);
Zhenyao Mo550c6002014-02-26 15:40:48 -0800212 root->traverse(&marker);
213 if (marker.samplerArrayIndexIsFloatLoopIndex())
214 {
215 infoSink.info.prefix(EPrefixError);
216 infoSink.info << "sampler array index is float loop index";
217 success = false;
218 }
219 }
zmo@google.com0c6bb7a2011-08-17 19:39:58 +0000220
zmo@google.com32e97312011-08-24 01:03:11 +0000221 // Built-in function emulation needs to happen after validateLimitations pass.
222 if (success && (compileOptions & SH_EMULATE_BUILT_IN_FUNCTIONS))
223 builtInFunctionEmulator.MarkBuiltInFunctionsForEmulation(root);
224
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000225 // Clamping uniform array bounds needs to happen after validateLimitations pass.
226 if (success && (compileOptions & SH_CLAMP_INDIRECT_ARRAY_BOUNDS))
227 arrayBoundsClamper.MarkIndirectArrayBoundsForClamping(root);
228
Jamie Madill183bde52014-07-02 15:31:19 -0400229 if (success && shaderType == GL_VERTEX_SHADER && (compileOptions & SH_INIT_GL_POSITION))
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800230 initializeGLPosition(root);
Zhenyao Moac44cd22013-09-23 14:57:09 -0400231
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800232 if (success && (compileOptions & SH_UNFOLD_SHORT_CIRCUIT))
233 {
Zhenyao Mo7cab38b2013-10-15 12:59:30 -0700234 UnfoldShortCircuitAST unfoldShortCircuit;
235 root->traverse(&unfoldShortCircuit);
236 unfoldShortCircuit.updateTree();
237 }
238
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800239 if (success && (compileOptions & SH_VARIABLES))
240 {
Zhenyao Mo74da9f22013-09-23 14:57:01 -0400241 collectVariables(root);
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800242 if (compileOptions & SH_ENFORCE_PACKING_RESTRICTIONS)
243 {
gman@chromium.org8d804792012-10-17 21:33:48 +0000244 success = enforcePackingRestrictions();
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800245 if (!success)
246 {
Jamie Madill075edd82013-07-08 13:30:19 -0400247 infoSink.info.prefix(EPrefixError);
248 infoSink.info << "too many uniforms";
gman@chromium.org8d804792012-10-17 21:33:48 +0000249 }
250 }
Jamie Madill183bde52014-07-02 15:31:19 -0400251 if (success && shaderType == GL_VERTEX_SHADER &&
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800252 (compileOptions & SH_INIT_VARYINGS_WITHOUT_STATIC_USE))
253 initializeVaryingsWithoutStaticUse(root);
gman@chromium.org8d804792012-10-17 21:33:48 +0000254 }
zmo@google.comfd747b82011-04-23 01:30:07 +0000255
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000256 if (success && (compileOptions & SH_INTERMEDIATE_TREE))
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000257 intermediate.outputTree(root);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000258
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000259 if (success && (compileOptions & SH_OBJECT_CODE))
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000260 translate(root);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000261 }
262
263 // Cleanup memory.
264 intermediate.remove(parseContext.treeRoot);
Zhenyao Mo7faf1a12014-04-25 18:03:56 -0700265 SetGlobalParseContext(NULL);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000266 return success;
267}
268
Nicolas Capens49a88872013-06-20 09:54:03 -0400269bool TCompiler::InitBuiltInSymbolTable(const ShBuiltInResources &resources)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000270{
shannon.woods%transgaming.com@gtempaccount.com18b4c4b2013-04-13 03:31:40 +0000271 compileResources = resources;
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400272 setResourceString();
shannonwoods@chromium.org2ac0be92013-05-30 00:02:27 +0000273
Nicolas Capens49a88872013-06-20 09:54:03 -0400274 assert(symbolTable.isEmpty());
275 symbolTable.push(); // COMMON_BUILTINS
276 symbolTable.push(); // ESSL1_BUILTINS
277 symbolTable.push(); // ESSL3_BUILTINS
shannonwoods@chromium.org2ac0be92013-05-30 00:02:27 +0000278
Nicolas Capens49a88872013-06-20 09:54:03 -0400279 TPublicType integer;
280 integer.type = EbtInt;
281 integer.primarySize = 1;
282 integer.secondarySize = 1;
283 integer.array = false;
284
285 TPublicType floatingPoint;
286 floatingPoint.type = EbtFloat;
287 floatingPoint.primarySize = 1;
288 floatingPoint.secondarySize = 1;
289 floatingPoint.array = false;
290
Zhenyao Moa5a1dfc2013-09-23 14:57:03 -0400291 TPublicType sampler;
292 sampler.primarySize = 1;
293 sampler.secondarySize = 1;
294 sampler.array = false;
295
Nicolas Capens49a88872013-06-20 09:54:03 -0400296 switch(shaderType)
297 {
Jamie Madill183bde52014-07-02 15:31:19 -0400298 case GL_FRAGMENT_SHADER:
Nicolas Capens49a88872013-06-20 09:54:03 -0400299 symbolTable.setDefaultPrecision(integer, EbpMedium);
300 break;
Jamie Madill183bde52014-07-02 15:31:19 -0400301 case GL_VERTEX_SHADER:
Nicolas Capens49a88872013-06-20 09:54:03 -0400302 symbolTable.setDefaultPrecision(integer, EbpHigh);
303 symbolTable.setDefaultPrecision(floatingPoint, EbpHigh);
304 break;
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800305 default:
306 assert(false && "Language not supported");
Nicolas Capens49a88872013-06-20 09:54:03 -0400307 }
Zhenyao Moa5a1dfc2013-09-23 14:57:03 -0400308 // We set defaults for all the sampler types, even those that are
309 // only available if an extension exists.
310 for (int samplerType = EbtGuardSamplerBegin + 1;
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800311 samplerType < EbtGuardSamplerEnd; ++samplerType)
312 {
Zhenyao Moa5a1dfc2013-09-23 14:57:03 -0400313 sampler.type = static_cast<TBasicType>(samplerType);
314 symbolTable.setDefaultPrecision(sampler, EbpLow);
315 }
Nicolas Capens49a88872013-06-20 09:54:03 -0400316
Jamie Madill1b452142013-07-12 14:51:11 -0400317 InsertBuiltInFunctions(shaderType, shaderSpec, resources, symbolTable);
Nicolas Capens49a88872013-06-20 09:54:03 -0400318
319 IdentifyBuiltIns(shaderType, shaderSpec, resources, symbolTable);
320
321 return true;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000322}
323
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400324void TCompiler::setResourceString()
325{
326 std::ostringstream strstream;
327 strstream << ":MaxVertexAttribs:" << compileResources.MaxVertexAttribs
328 << ":MaxVertexUniformVectors:" << compileResources.MaxVertexUniformVectors
329 << ":MaxVaryingVectors:" << compileResources.MaxVaryingVectors
330 << ":MaxVertexTextureImageUnits:" << compileResources.MaxVertexTextureImageUnits
331 << ":MaxCombinedTextureImageUnits:" << compileResources.MaxCombinedTextureImageUnits
332 << ":MaxTextureImageUnits:" << compileResources.MaxTextureImageUnits
333 << ":MaxFragmentUniformVectors:" << compileResources.MaxFragmentUniformVectors
334 << ":MaxDrawBuffers:" << compileResources.MaxDrawBuffers
335 << ":OES_standard_derivatives:" << compileResources.OES_standard_derivatives
336 << ":OES_EGL_image_external:" << compileResources.OES_EGL_image_external
337 << ":ARB_texture_rectangle:" << compileResources.ARB_texture_rectangle
338 << ":EXT_draw_buffers:" << compileResources.EXT_draw_buffers
339 << ":FragmentPrecisionHigh:" << compileResources.FragmentPrecisionHigh
340 << ":MaxExpressionComplexity:" << compileResources.MaxExpressionComplexity
341 << ":MaxCallStackDepth:" << compileResources.MaxCallStackDepth
342 << ":EXT_frag_depth:" << compileResources.EXT_frag_depth
343 << ":EXT_shader_texture_lod:" << compileResources.EXT_shader_texture_lod
344 << ":MaxVertexOutputVectors:" << compileResources.MaxVertexOutputVectors
345 << ":MaxFragmentInputVectors:" << compileResources.MaxFragmentInputVectors
346 << ":MinProgramTexelOffset:" << compileResources.MinProgramTexelOffset
347 << ":MaxProgramTexelOffset:" << compileResources.MaxProgramTexelOffset;
348
349 builtInResourcesString = strstream.str();
350}
351
alokp@chromium.org07620a52010-09-23 17:53:56 +0000352void TCompiler::clearResults()
353{
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000354 arrayBoundsClamper.Cleanup();
alokp@chromium.org07620a52010-09-23 17:53:56 +0000355 infoSink.info.erase();
356 infoSink.obj.erase();
357 infoSink.debug.erase();
358
359 attribs.clear();
360 uniforms.clear();
Zhenyao Mod2d340b2013-09-23 14:57:05 -0400361 varyings.clear();
zmo@google.coma3b4ab42011-09-16 00:53:26 +0000362
363 builtInFunctionEmulator.Cleanup();
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +0000364
365 nameMap.clear();
alokp@chromium.org07620a52010-09-23 17:53:56 +0000366}
367
Jamie Madilleb1a0102013-07-08 13:31:38 -0400368bool TCompiler::detectCallDepth(TIntermNode* root, TInfoSink& infoSink, bool limitCallStackDepth)
zmo@google.comb1762df2011-07-30 02:04:23 +0000369{
Jamie Madilleb1a0102013-07-08 13:31:38 -0400370 DetectCallDepth detect(infoSink, limitCallStackDepth, maxCallStackDepth);
zmo@google.comb1762df2011-07-30 02:04:23 +0000371 root->traverse(&detect);
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800372 switch (detect.detectCallDepth())
373 {
374 case DetectCallDepth::kErrorNone:
375 return true;
376 case DetectCallDepth::kErrorMissingMain:
377 infoSink.info.prefix(EPrefixError);
378 infoSink.info << "Missing main()";
379 return false;
380 case DetectCallDepth::kErrorRecursion:
381 infoSink.info.prefix(EPrefixError);
382 infoSink.info << "Function recursion detected";
383 return false;
384 case DetectCallDepth::kErrorMaxDepthExceeded:
385 infoSink.info.prefix(EPrefixError);
386 infoSink.info << "Function call stack too deep";
387 return false;
388 default:
389 UNREACHABLE();
390 return false;
zmo@google.comb1762df2011-07-30 02:04:23 +0000391 }
392}
393
Jamie Madill05a80ce2013-06-20 11:55:49 -0400394bool TCompiler::validateOutputs(TIntermNode* root)
395{
396 ValidateOutputs validateOutputs(infoSink.info, compileResources.MaxDrawBuffers);
397 root->traverse(&validateOutputs);
398 return (validateOutputs.numErrors() == 0);
399}
400
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +0000401void TCompiler::rewriteCSSShader(TIntermNode* root)
402{
403 RenameFunction renamer("main(", "css_main(");
404 root->traverse(&renamer);
405}
406
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800407bool TCompiler::validateLimitations(TIntermNode* root)
408{
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000409 ValidateLimitations validate(shaderType, infoSink.info);
410 root->traverse(&validate);
411 return validate.numErrors() == 0;
412}
413
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000414bool TCompiler::enforceTimingRestrictions(TIntermNode* root, bool outputGraph)
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000415{
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800416 if (shaderSpec != SH_WEBGL_SPEC)
417 {
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000418 infoSink.info << "Timing restrictions must be enforced under the WebGL spec.";
419 return false;
420 }
421
Jamie Madill183bde52014-07-02 15:31:19 -0400422 if (shaderType == GL_FRAGMENT_SHADER)
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800423 {
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000424 TDependencyGraph graph(root);
425
426 // Output any errors first.
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000427 bool success = enforceFragmentShaderTimingRestrictions(graph);
Jamie Madill5508f392014-02-20 13:31:36 -0500428
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000429 // Then, output the dependency graph.
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800430 if (outputGraph)
431 {
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000432 TDependencyGraphOutput output(infoSink.info);
433 output.outputAllSpanningTrees(graph);
434 }
Jamie Madill5508f392014-02-20 13:31:36 -0500435
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000436 return success;
437 }
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800438 else
439 {
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000440 return enforceVertexShaderTimingRestrictions(root);
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000441 }
442}
443
Jamie Madilleb1a0102013-07-08 13:31:38 -0400444bool TCompiler::limitExpressionComplexity(TIntermNode* root)
445{
Jamie Madill6654bc92014-03-26 14:01:57 -0400446 TMaxDepthTraverser traverser(maxExpressionComplexity+1);
Jamie Madilleb1a0102013-07-08 13:31:38 -0400447 root->traverse(&traverser);
Jamie Madill6654bc92014-03-26 14:01:57 -0400448
449 if (traverser.getMaxDepth() > maxExpressionComplexity)
450 {
451 infoSink.info << "Expression too complex.";
452 return false;
453 }
454
Jamie Madilleb1a0102013-07-08 13:31:38 -0400455 TDependencyGraph graph(root);
456
457 for (TFunctionCallVector::const_iterator iter = graph.beginUserDefinedFunctionCalls();
458 iter != graph.endUserDefinedFunctionCalls();
459 ++iter)
460 {
461 TGraphFunctionCall* samplerSymbol = *iter;
462 TDependencyGraphTraverser graphTraverser;
463 samplerSymbol->traverse(&graphTraverser);
464 }
465
Jamie Madilleb1a0102013-07-08 13:31:38 -0400466 return true;
467}
468
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000469bool TCompiler::enforceFragmentShaderTimingRestrictions(const TDependencyGraph& graph)
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000470{
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000471 RestrictFragmentShaderTiming restrictor(infoSink.info);
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000472 restrictor.enforceRestrictions(graph);
473 return restrictor.numErrors() == 0;
474}
475
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000476bool TCompiler::enforceVertexShaderTimingRestrictions(TIntermNode* root)
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000477{
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000478 RestrictVertexShaderTiming restrictor(infoSink.info);
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000479 restrictor.enforceRestrictions(root);
480 return restrictor.numErrors() == 0;
481}
482
Zhenyao Mo74da9f22013-09-23 14:57:01 -0400483void TCompiler::collectVariables(TIntermNode* root)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000484{
Zhenyao Mo74da9f22013-09-23 14:57:01 -0400485 CollectVariables collect(attribs, uniforms, varyings, hashFunction);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000486 root->traverse(&collect);
487}
zmo@google.comfd747b82011-04-23 01:30:07 +0000488
gman@chromium.org8d804792012-10-17 21:33:48 +0000489bool TCompiler::enforcePackingRestrictions()
490{
491 VariablePacker packer;
492 return packer.CheckVariablesWithinPackingLimits(maxUniformVectors, uniforms);
493}
494
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800495void TCompiler::initializeGLPosition(TIntermNode* root)
496{
497 InitializeVariables::InitVariableInfoList variables;
498 InitializeVariables::InitVariableInfo var(
499 "gl_Position", TType(EbtFloat, EbpUndefined, EvqPosition, 4));
500 variables.push_back(var);
501 InitializeVariables initializer(variables);
502 root->traverse(&initializer);
503}
504
505void TCompiler::initializeVaryingsWithoutStaticUse(TIntermNode* root)
506{
507 InitializeVariables::InitVariableInfoList variables;
508 for (size_t ii = 0; ii < varyings.size(); ++ii)
509 {
510 const TVariableInfo& varying = varyings[ii];
511 if (varying.staticUse)
512 continue;
Zhenyao Mo66e5dda2014-04-28 11:13:25 -0700513 unsigned char primarySize = 1, secondarySize = 1;
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800514 switch (varying.type)
515 {
Jamie Madill183bde52014-07-02 15:31:19 -0400516 case GL_FLOAT:
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800517 break;
Jamie Madill183bde52014-07-02 15:31:19 -0400518 case GL_FLOAT_VEC2:
Zhenyao Mo66e5dda2014-04-28 11:13:25 -0700519 primarySize = 2;
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800520 break;
Jamie Madill183bde52014-07-02 15:31:19 -0400521 case GL_FLOAT_VEC3:
Zhenyao Mo66e5dda2014-04-28 11:13:25 -0700522 primarySize = 3;
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800523 break;
Jamie Madill183bde52014-07-02 15:31:19 -0400524 case GL_FLOAT_VEC4:
Zhenyao Mo66e5dda2014-04-28 11:13:25 -0700525 primarySize = 4;
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800526 break;
Jamie Madill183bde52014-07-02 15:31:19 -0400527 case GL_FLOAT_MAT2:
Zhenyao Mo66e5dda2014-04-28 11:13:25 -0700528 primarySize = 2;
529 secondarySize = 2;
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800530 break;
Jamie Madill183bde52014-07-02 15:31:19 -0400531 case GL_FLOAT_MAT3:
Zhenyao Mo66e5dda2014-04-28 11:13:25 -0700532 primarySize = 3;
533 secondarySize = 3;
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800534 break;
Jamie Madill183bde52014-07-02 15:31:19 -0400535 case GL_FLOAT_MAT4:
Zhenyao Mo66e5dda2014-04-28 11:13:25 -0700536 primarySize = 4;
537 secondarySize = 4;
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800538 break;
539 default:
540 ASSERT(false);
541 }
Zhenyao Mo66e5dda2014-04-28 11:13:25 -0700542 TType type(EbtFloat, EbpUndefined, EvqVaryingOut, primarySize, secondarySize, varying.isArray);
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800543 TString name = varying.name.c_str();
544 if (varying.isArray)
545 {
546 type.setArraySize(varying.size);
547 name = name.substr(0, name.find_first_of('['));
548 }
549
550 InitializeVariables::InitVariableInfo var(name, type);
551 variables.push_back(var);
552 }
553 InitializeVariables initializer(variables);
554 root->traverse(&initializer);
555}
556
zmo@google.com5601ea02011-06-10 18:23:25 +0000557const TExtensionBehavior& TCompiler::getExtensionBehavior() const
558{
559 return extensionBehavior;
560}
zmo@google.com32e97312011-08-24 01:03:11 +0000561
shannon.woods%transgaming.com@gtempaccount.com18b4c4b2013-04-13 03:31:40 +0000562const ShBuiltInResources& TCompiler::getResources() const
563{
564 return compileResources;
565}
566
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000567const ArrayBoundsClamper& TCompiler::getArrayBoundsClamper() const
568{
569 return arrayBoundsClamper;
570}
571
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000572ShArrayIndexClampingStrategy TCompiler::getArrayIndexClampingStrategy() const
573{
574 return clampingStrategy;
575}
576
577const BuiltInFunctionEmulator& TCompiler::getBuiltInFunctionEmulator() const
578{
579 return builtInFunctionEmulator;
580}