blob: 0606c72e393f3f42b4ba43205816759e96a558e2 [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 Mocd68fe72014-07-11 10:45:44 -070016#include "compiler/translator/ScalarizeVecAndMatConstructorArgs.h"
Zhenyao Mo7cab38b2013-10-15 12:59:30 -070017#include "compiler/translator/UnfoldShortCircuitAST.h"
Geoff Lang17732822013-08-29 13:46:49 -040018#include "compiler/translator/ValidateLimitations.h"
19#include "compiler/translator/ValidateOutputs.h"
20#include "compiler/translator/VariablePacker.h"
21#include "compiler/translator/depgraph/DependencyGraph.h"
22#include "compiler/translator/depgraph/DependencyGraphOutput.h"
23#include "compiler/translator/timing/RestrictFragmentShaderTiming.h"
24#include "compiler/translator/timing/RestrictVertexShaderTiming.h"
shannon.woods@transgaming.comda1ed362013-01-25 21:54:57 +000025#include "third_party/compiler/ArrayBoundsClamper.h"
Jamie Madill183bde52014-07-02 15:31:19 -040026#include "angle_gl.h"
Jamie Madillaa72d782014-07-02 15:31:19 -040027#include "common/utilities.h"
alokp@chromium.org07620a52010-09-23 17:53:56 +000028
Jamie Madill5508f392014-02-20 13:31:36 -050029bool IsWebGLBasedSpec(ShShaderSpec spec)
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +000030{
31 return spec == SH_WEBGL_SPEC || spec == SH_CSS_SHADERS_SPEC;
32}
33
Zhenyao Mo7faf1a12014-04-25 18:03:56 -070034size_t GetGlobalMaxTokenSize(ShShaderSpec spec)
Jamie Madill88f6e942014-02-19 10:27:53 -050035{
Jamie Madill88f6e942014-02-19 10:27:53 -050036 // WebGL defines a max token legnth of 256, while ES2 leaves max token
37 // size undefined. ES3 defines a max size of 1024 characters.
Zhenyao Mo7faf1a12014-04-25 18:03:56 -070038 if (IsWebGLBasedSpec(spec))
Jamie Madill88f6e942014-02-19 10:27:53 -050039 {
40 return 256;
41 }
42 else
43 {
44 return 1024;
45 }
46}
47
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +000048namespace {
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080049class TScopedPoolAllocator
50{
51 public:
52 TScopedPoolAllocator(TPoolAllocator* allocator) : mAllocator(allocator)
53 {
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -040054 mAllocator->push();
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +000055 SetGlobalPoolAllocator(mAllocator);
56 }
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080057 ~TScopedPoolAllocator()
58 {
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +000059 SetGlobalPoolAllocator(NULL);
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -040060 mAllocator->pop();
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +000061 }
62
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080063 private:
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +000064 TPoolAllocator* mAllocator;
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -040065};
66
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080067class TScopedSymbolTableLevel
68{
69 public:
70 TScopedSymbolTableLevel(TSymbolTable* table) : mTable(table)
71 {
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -040072 ASSERT(mTable->atBuiltInLevel());
73 mTable->push();
74 }
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080075 ~TScopedSymbolTableLevel()
76 {
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -040077 while (!mTable->atBuiltInLevel())
78 mTable->pop();
79 }
80
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080081 private:
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -040082 TSymbolTable* mTable;
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +000083};
84} // namespace
85
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080086TShHandleBase::TShHandleBase()
87{
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +000088 allocator.push();
89 SetGlobalPoolAllocator(&allocator);
90}
91
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080092TShHandleBase::~TShHandleBase()
93{
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +000094 SetGlobalPoolAllocator(NULL);
95 allocator.popAll();
96}
97
Jamie Madill183bde52014-07-02 15:31:19 -040098TCompiler::TCompiler(sh::GLenum type, ShShaderSpec spec, ShShaderOutput output)
alokp@chromium.org4888ceb2010-10-01 21:13:12 +000099 : shaderType(type),
zmo@google.comf420c422011-09-12 18:27:59 +0000100 shaderSpec(spec),
Jamie Madill68fe74a2014-05-27 12:56:01 -0400101 outputType(output),
Jamie Madilleb1a0102013-07-08 13:31:38 -0400102 maxUniformVectors(0),
103 maxExpressionComplexity(0),
104 maxCallStackDepth(0),
shannon.woods%transgaming.com@gtempaccount.comcbb6b6a2013-04-13 03:27:47 +0000105 fragmentPrecisionHigh(false),
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000106 clampingStrategy(SH_CLAMP_WITH_CLAMP_INTRINSIC),
zmo@google.com9996b8e2012-01-19 01:43:55 +0000107 builtInFunctionEmulator(type)
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000108{
109}
110
111TCompiler::~TCompiler()
112{
113}
114
115bool TCompiler::Init(const ShBuiltInResources& resources)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000116{
shannon.woods%transgaming.com@gtempaccount.com0bbed382013-04-13 03:38:07 +0000117 shaderVersion = 100;
Jamie Madill183bde52014-07-02 15:31:19 -0400118 maxUniformVectors = (shaderType == GL_VERTEX_SHADER) ?
gman@chromium.org8d804792012-10-17 21:33:48 +0000119 resources.MaxVertexUniformVectors :
120 resources.MaxFragmentUniformVectors;
Jamie Madilleb1a0102013-07-08 13:31:38 -0400121 maxExpressionComplexity = resources.MaxExpressionComplexity;
122 maxCallStackDepth = resources.MaxCallStackDepth;
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400123
124 SetGlobalPoolAllocator(&allocator);
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000125
alokp@chromium.org07620a52010-09-23 17:53:56 +0000126 // Generate built-in symbol table.
127 if (!InitBuiltInSymbolTable(resources))
128 return false;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000129 InitExtensionBehavior(resources, extensionBehavior);
shannon.woods%transgaming.com@gtempaccount.comcbb6b6a2013-04-13 03:27:47 +0000130 fragmentPrecisionHigh = resources.FragmentPrecisionHigh == 1;
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000131
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000132 arrayBoundsClamper.SetClampingStrategy(resources.ArrayIndexClampingStrategy);
133 clampingStrategy = resources.ArrayIndexClampingStrategy;
134
daniel@transgaming.comc23f4612012-11-28 19:42:57 +0000135 hashFunction = resources.HashFunction;
136
alokp@chromium.org07620a52010-09-23 17:53:56 +0000137 return true;
138}
139
140bool TCompiler::compile(const char* const shaderStrings[],
shannon.woods@transgaming.comd64b3da2013-02-28 23:19:26 +0000141 size_t numStrings,
alokp@chromium.org07620a52010-09-23 17:53:56 +0000142 int compileOptions)
143{
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400144 TScopedPoolAllocator scopedAlloc(&allocator);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000145 clearResults();
146
147 if (numStrings == 0)
148 return true;
149
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000150 // If compiling for WebGL, validate loop and indexing as well.
Jamie Madill5508f392014-02-20 13:31:36 -0500151 if (IsWebGLBasedSpec(shaderSpec))
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000152 compileOptions |= SH_VALIDATE_LOOP_INDEXING;
alokp@chromium.org1f299542010-11-12 15:50:23 +0000153
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000154 // First string is path of source file if flag is set. The actual source follows.
155 const char* sourcePath = NULL;
shannon.woods@transgaming.comd64b3da2013-02-28 23:19:26 +0000156 size_t firstSource = 0;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000157 if (compileOptions & SH_SOURCE_PATH)
158 {
159 sourcePath = shaderStrings[0];
160 ++firstSource;
161 }
162
alokp@chromium.org07620a52010-09-23 17:53:56 +0000163 TIntermediate intermediate(infoSink);
164 TParseContext parseContext(symbolTable, extensionBehavior, intermediate,
zmo@google.comdc4b4f82011-06-17 00:42:53 +0000165 shaderType, shaderSpec, compileOptions, true,
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000166 sourcePath, infoSink);
shannon.woods%transgaming.com@gtempaccount.comcbb6b6a2013-04-13 03:27:47 +0000167 parseContext.fragmentPrecisionHigh = fragmentPrecisionHigh;
Alok Priyadarshi8156b6b2013-09-23 14:56:58 -0400168 SetGlobalParseContext(&parseContext);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000169
170 // We preserve symbols at the built-in level from compile-to-compile.
171 // Start pushing the user-defined symbols at global level.
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400172 TScopedSymbolTableLevel scopedSymbolLevel(&symbolTable);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000173
174 // Parse shader.
175 bool success =
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000176 (PaParseStrings(numStrings - firstSource, &shaderStrings[firstSource], NULL, &parseContext) == 0) &&
alokp@chromium.org07620a52010-09-23 17:53:56 +0000177 (parseContext.treeRoot != NULL);
shannon.woods%transgaming.com@gtempaccount.com0bbed382013-04-13 03:38:07 +0000178
shannon.woods%transgaming.com@gtempaccount.com5524db02013-04-13 03:38:16 +0000179 shaderVersion = parseContext.getShaderVersion();
shannon.woods%transgaming.com@gtempaccount.com0bbed382013-04-13 03:38:07 +0000180
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800181 if (success)
182 {
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000183 TIntermNode* root = parseContext.treeRoot;
184 success = intermediate.postProcess(root);
185
Jamie Madill6654bc92014-03-26 14:01:57 -0400186 // Disallow expressions deemed too complex.
187 if (success && (compileOptions & SH_LIMIT_EXPRESSION_COMPLEXITY))
188 success = limitExpressionComplexity(root);
189
zmo@google.comb1762df2011-07-30 02:04:23 +0000190 if (success)
Jamie Madilleb1a0102013-07-08 13:31:38 -0400191 success = detectCallDepth(root, infoSink, (compileOptions & SH_LIMIT_CALL_STACK_DEPTH) != 0);
zmo@google.comb1762df2011-07-30 02:04:23 +0000192
Jamie Madill183bde52014-07-02 15:31:19 -0400193 if (success && shaderVersion == 300 && shaderType == GL_FRAGMENT_SHADER)
Jamie Madill05a80ce2013-06-20 11:55:49 -0400194 success = validateOutputs(root);
195
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000196 if (success && (compileOptions & SH_VALIDATE_LOOP_INDEXING))
197 success = validateLimitations(root);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000198
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000199 if (success && (compileOptions & SH_TIMING_RESTRICTIONS))
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000200 success = enforceTimingRestrictions(root, (compileOptions & SH_DEPENDENCY_GRAPH) != 0);
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000201
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +0000202 if (success && shaderSpec == SH_CSS_SHADERS_SPEC)
203 rewriteCSSShader(root);
204
zmo@google.com0c6bb7a2011-08-17 19:39:58 +0000205 // Unroll for-loop markup needs to happen after validateLimitations pass.
206 if (success && (compileOptions & SH_UNROLL_FOR_LOOP_WITH_INTEGER_INDEX))
Zhenyao Mo3cdfcce2014-03-07 13:00:08 -0800207 {
208 ForLoopUnrollMarker marker(ForLoopUnrollMarker::kIntegerIndex);
Zhenyao Mo550c6002014-02-26 15:40:48 -0800209 root->traverse(&marker);
210 }
211 if (success && (compileOptions & SH_UNROLL_FOR_LOOP_WITH_SAMPLER_ARRAY_INDEX))
Zhenyao Mo3cdfcce2014-03-07 13:00:08 -0800212 {
213 ForLoopUnrollMarker marker(ForLoopUnrollMarker::kSamplerArrayIndex);
Zhenyao Mo550c6002014-02-26 15:40:48 -0800214 root->traverse(&marker);
215 if (marker.samplerArrayIndexIsFloatLoopIndex())
216 {
217 infoSink.info.prefix(EPrefixError);
218 infoSink.info << "sampler array index is float loop index";
219 success = false;
220 }
221 }
zmo@google.com0c6bb7a2011-08-17 19:39:58 +0000222
zmo@google.com32e97312011-08-24 01:03:11 +0000223 // Built-in function emulation needs to happen after validateLimitations pass.
224 if (success && (compileOptions & SH_EMULATE_BUILT_IN_FUNCTIONS))
225 builtInFunctionEmulator.MarkBuiltInFunctionsForEmulation(root);
226
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000227 // Clamping uniform array bounds needs to happen after validateLimitations pass.
228 if (success && (compileOptions & SH_CLAMP_INDIRECT_ARRAY_BOUNDS))
229 arrayBoundsClamper.MarkIndirectArrayBoundsForClamping(root);
230
Jamie Madill183bde52014-07-02 15:31:19 -0400231 if (success && shaderType == GL_VERTEX_SHADER && (compileOptions & SH_INIT_GL_POSITION))
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800232 initializeGLPosition(root);
Zhenyao Moac44cd22013-09-23 14:57:09 -0400233
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800234 if (success && (compileOptions & SH_UNFOLD_SHORT_CIRCUIT))
235 {
Zhenyao Mo7cab38b2013-10-15 12:59:30 -0700236 UnfoldShortCircuitAST unfoldShortCircuit;
237 root->traverse(&unfoldShortCircuit);
238 unfoldShortCircuit.updateTree();
239 }
240
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800241 if (success && (compileOptions & SH_VARIABLES))
242 {
Zhenyao Mo74da9f22013-09-23 14:57:01 -0400243 collectVariables(root);
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800244 if (compileOptions & SH_ENFORCE_PACKING_RESTRICTIONS)
245 {
gman@chromium.org8d804792012-10-17 21:33:48 +0000246 success = enforcePackingRestrictions();
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800247 if (!success)
248 {
Jamie Madill075edd82013-07-08 13:30:19 -0400249 infoSink.info.prefix(EPrefixError);
250 infoSink.info << "too many uniforms";
gman@chromium.org8d804792012-10-17 21:33:48 +0000251 }
252 }
Jamie Madill183bde52014-07-02 15:31:19 -0400253 if (success && shaderType == GL_VERTEX_SHADER &&
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800254 (compileOptions & SH_INIT_VARYINGS_WITHOUT_STATIC_USE))
255 initializeVaryingsWithoutStaticUse(root);
gman@chromium.org8d804792012-10-17 21:33:48 +0000256 }
zmo@google.comfd747b82011-04-23 01:30:07 +0000257
Zhenyao Mocd68fe72014-07-11 10:45:44 -0700258 if (success && (compileOptions & SH_SCALARIZE_VEC_AND_MAT_CONSTRUCTOR_ARGS))
259 {
260 ScalarizeVecAndMatConstructorArgs scalarizer;
261 root->traverse(&scalarizer);
262 }
263
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000264 if (success && (compileOptions & SH_INTERMEDIATE_TREE))
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000265 intermediate.outputTree(root);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000266
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000267 if (success && (compileOptions & SH_OBJECT_CODE))
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000268 translate(root);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000269 }
270
271 // Cleanup memory.
272 intermediate.remove(parseContext.treeRoot);
Zhenyao Mo7faf1a12014-04-25 18:03:56 -0700273 SetGlobalParseContext(NULL);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000274 return success;
275}
276
Nicolas Capens49a88872013-06-20 09:54:03 -0400277bool TCompiler::InitBuiltInSymbolTable(const ShBuiltInResources &resources)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000278{
shannon.woods%transgaming.com@gtempaccount.com18b4c4b2013-04-13 03:31:40 +0000279 compileResources = resources;
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400280 setResourceString();
shannonwoods@chromium.org2ac0be92013-05-30 00:02:27 +0000281
Nicolas Capens49a88872013-06-20 09:54:03 -0400282 assert(symbolTable.isEmpty());
283 symbolTable.push(); // COMMON_BUILTINS
284 symbolTable.push(); // ESSL1_BUILTINS
285 symbolTable.push(); // ESSL3_BUILTINS
shannonwoods@chromium.org2ac0be92013-05-30 00:02:27 +0000286
Nicolas Capens49a88872013-06-20 09:54:03 -0400287 TPublicType integer;
288 integer.type = EbtInt;
289 integer.primarySize = 1;
290 integer.secondarySize = 1;
291 integer.array = false;
292
293 TPublicType floatingPoint;
294 floatingPoint.type = EbtFloat;
295 floatingPoint.primarySize = 1;
296 floatingPoint.secondarySize = 1;
297 floatingPoint.array = false;
298
Zhenyao Moa5a1dfc2013-09-23 14:57:03 -0400299 TPublicType sampler;
300 sampler.primarySize = 1;
301 sampler.secondarySize = 1;
302 sampler.array = false;
303
Nicolas Capens49a88872013-06-20 09:54:03 -0400304 switch(shaderType)
305 {
Jamie Madill183bde52014-07-02 15:31:19 -0400306 case GL_FRAGMENT_SHADER:
Nicolas Capens49a88872013-06-20 09:54:03 -0400307 symbolTable.setDefaultPrecision(integer, EbpMedium);
308 break;
Jamie Madill183bde52014-07-02 15:31:19 -0400309 case GL_VERTEX_SHADER:
Nicolas Capens49a88872013-06-20 09:54:03 -0400310 symbolTable.setDefaultPrecision(integer, EbpHigh);
311 symbolTable.setDefaultPrecision(floatingPoint, EbpHigh);
312 break;
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800313 default:
314 assert(false && "Language not supported");
Nicolas Capens49a88872013-06-20 09:54:03 -0400315 }
Zhenyao Moa5a1dfc2013-09-23 14:57:03 -0400316 // We set defaults for all the sampler types, even those that are
317 // only available if an extension exists.
318 for (int samplerType = EbtGuardSamplerBegin + 1;
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800319 samplerType < EbtGuardSamplerEnd; ++samplerType)
320 {
Zhenyao Moa5a1dfc2013-09-23 14:57:03 -0400321 sampler.type = static_cast<TBasicType>(samplerType);
322 symbolTable.setDefaultPrecision(sampler, EbpLow);
323 }
Nicolas Capens49a88872013-06-20 09:54:03 -0400324
Jamie Madill1b452142013-07-12 14:51:11 -0400325 InsertBuiltInFunctions(shaderType, shaderSpec, resources, symbolTable);
Nicolas Capens49a88872013-06-20 09:54:03 -0400326
327 IdentifyBuiltIns(shaderType, shaderSpec, resources, symbolTable);
328
329 return true;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000330}
331
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400332void TCompiler::setResourceString()
333{
334 std::ostringstream strstream;
335 strstream << ":MaxVertexAttribs:" << compileResources.MaxVertexAttribs
336 << ":MaxVertexUniformVectors:" << compileResources.MaxVertexUniformVectors
337 << ":MaxVaryingVectors:" << compileResources.MaxVaryingVectors
338 << ":MaxVertexTextureImageUnits:" << compileResources.MaxVertexTextureImageUnits
339 << ":MaxCombinedTextureImageUnits:" << compileResources.MaxCombinedTextureImageUnits
340 << ":MaxTextureImageUnits:" << compileResources.MaxTextureImageUnits
341 << ":MaxFragmentUniformVectors:" << compileResources.MaxFragmentUniformVectors
342 << ":MaxDrawBuffers:" << compileResources.MaxDrawBuffers
343 << ":OES_standard_derivatives:" << compileResources.OES_standard_derivatives
344 << ":OES_EGL_image_external:" << compileResources.OES_EGL_image_external
345 << ":ARB_texture_rectangle:" << compileResources.ARB_texture_rectangle
346 << ":EXT_draw_buffers:" << compileResources.EXT_draw_buffers
347 << ":FragmentPrecisionHigh:" << compileResources.FragmentPrecisionHigh
348 << ":MaxExpressionComplexity:" << compileResources.MaxExpressionComplexity
349 << ":MaxCallStackDepth:" << compileResources.MaxCallStackDepth
350 << ":EXT_frag_depth:" << compileResources.EXT_frag_depth
351 << ":EXT_shader_texture_lod:" << compileResources.EXT_shader_texture_lod
352 << ":MaxVertexOutputVectors:" << compileResources.MaxVertexOutputVectors
353 << ":MaxFragmentInputVectors:" << compileResources.MaxFragmentInputVectors
354 << ":MinProgramTexelOffset:" << compileResources.MinProgramTexelOffset
355 << ":MaxProgramTexelOffset:" << compileResources.MaxProgramTexelOffset;
356
357 builtInResourcesString = strstream.str();
358}
359
alokp@chromium.org07620a52010-09-23 17:53:56 +0000360void TCompiler::clearResults()
361{
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000362 arrayBoundsClamper.Cleanup();
alokp@chromium.org07620a52010-09-23 17:53:56 +0000363 infoSink.info.erase();
364 infoSink.obj.erase();
365 infoSink.debug.erase();
366
Jamie Madilled27c722014-07-02 15:31:23 -0400367 attributes.clear();
368 outputVariables.clear();
alokp@chromium.org07620a52010-09-23 17:53:56 +0000369 uniforms.clear();
Jamie Madill23a8a432014-07-09 13:27:42 -0400370 expandedUniforms.clear();
Zhenyao Mod2d340b2013-09-23 14:57:05 -0400371 varyings.clear();
Jamie Madill23a8a432014-07-09 13:27:42 -0400372 expandedVaryings.clear();
Jamie Madilled27c722014-07-02 15:31:23 -0400373 interfaceBlocks.clear();
zmo@google.coma3b4ab42011-09-16 00:53:26 +0000374
375 builtInFunctionEmulator.Cleanup();
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +0000376
377 nameMap.clear();
alokp@chromium.org07620a52010-09-23 17:53:56 +0000378}
379
Jamie Madilleb1a0102013-07-08 13:31:38 -0400380bool TCompiler::detectCallDepth(TIntermNode* root, TInfoSink& infoSink, bool limitCallStackDepth)
zmo@google.comb1762df2011-07-30 02:04:23 +0000381{
Jamie Madilleb1a0102013-07-08 13:31:38 -0400382 DetectCallDepth detect(infoSink, limitCallStackDepth, maxCallStackDepth);
zmo@google.comb1762df2011-07-30 02:04:23 +0000383 root->traverse(&detect);
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800384 switch (detect.detectCallDepth())
385 {
386 case DetectCallDepth::kErrorNone:
387 return true;
388 case DetectCallDepth::kErrorMissingMain:
389 infoSink.info.prefix(EPrefixError);
390 infoSink.info << "Missing main()";
391 return false;
392 case DetectCallDepth::kErrorRecursion:
393 infoSink.info.prefix(EPrefixError);
394 infoSink.info << "Function recursion detected";
395 return false;
396 case DetectCallDepth::kErrorMaxDepthExceeded:
397 infoSink.info.prefix(EPrefixError);
398 infoSink.info << "Function call stack too deep";
399 return false;
400 default:
401 UNREACHABLE();
402 return false;
zmo@google.comb1762df2011-07-30 02:04:23 +0000403 }
404}
405
Jamie Madill05a80ce2013-06-20 11:55:49 -0400406bool TCompiler::validateOutputs(TIntermNode* root)
407{
408 ValidateOutputs validateOutputs(infoSink.info, compileResources.MaxDrawBuffers);
409 root->traverse(&validateOutputs);
410 return (validateOutputs.numErrors() == 0);
411}
412
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +0000413void TCompiler::rewriteCSSShader(TIntermNode* root)
414{
415 RenameFunction renamer("main(", "css_main(");
416 root->traverse(&renamer);
417}
418
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800419bool TCompiler::validateLimitations(TIntermNode* root)
420{
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000421 ValidateLimitations validate(shaderType, infoSink.info);
422 root->traverse(&validate);
423 return validate.numErrors() == 0;
424}
425
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000426bool TCompiler::enforceTimingRestrictions(TIntermNode* root, bool outputGraph)
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000427{
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800428 if (shaderSpec != SH_WEBGL_SPEC)
429 {
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000430 infoSink.info << "Timing restrictions must be enforced under the WebGL spec.";
431 return false;
432 }
433
Jamie Madill183bde52014-07-02 15:31:19 -0400434 if (shaderType == GL_FRAGMENT_SHADER)
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800435 {
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000436 TDependencyGraph graph(root);
437
438 // Output any errors first.
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000439 bool success = enforceFragmentShaderTimingRestrictions(graph);
Jamie Madill5508f392014-02-20 13:31:36 -0500440
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000441 // Then, output the dependency graph.
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800442 if (outputGraph)
443 {
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000444 TDependencyGraphOutput output(infoSink.info);
445 output.outputAllSpanningTrees(graph);
446 }
Jamie Madill5508f392014-02-20 13:31:36 -0500447
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000448 return success;
449 }
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800450 else
451 {
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000452 return enforceVertexShaderTimingRestrictions(root);
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000453 }
454}
455
Jamie Madilleb1a0102013-07-08 13:31:38 -0400456bool TCompiler::limitExpressionComplexity(TIntermNode* root)
457{
Jamie Madill6654bc92014-03-26 14:01:57 -0400458 TMaxDepthTraverser traverser(maxExpressionComplexity+1);
Jamie Madilleb1a0102013-07-08 13:31:38 -0400459 root->traverse(&traverser);
Jamie Madill6654bc92014-03-26 14:01:57 -0400460
461 if (traverser.getMaxDepth() > maxExpressionComplexity)
462 {
463 infoSink.info << "Expression too complex.";
464 return false;
465 }
466
Jamie Madilleb1a0102013-07-08 13:31:38 -0400467 TDependencyGraph graph(root);
468
469 for (TFunctionCallVector::const_iterator iter = graph.beginUserDefinedFunctionCalls();
470 iter != graph.endUserDefinedFunctionCalls();
471 ++iter)
472 {
473 TGraphFunctionCall* samplerSymbol = *iter;
474 TDependencyGraphTraverser graphTraverser;
475 samplerSymbol->traverse(&graphTraverser);
476 }
477
Jamie Madilleb1a0102013-07-08 13:31:38 -0400478 return true;
479}
480
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000481bool TCompiler::enforceFragmentShaderTimingRestrictions(const TDependencyGraph& graph)
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000482{
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000483 RestrictFragmentShaderTiming restrictor(infoSink.info);
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000484 restrictor.enforceRestrictions(graph);
485 return restrictor.numErrors() == 0;
486}
487
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000488bool TCompiler::enforceVertexShaderTimingRestrictions(TIntermNode* root)
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000489{
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000490 RestrictVertexShaderTiming restrictor(infoSink.info);
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000491 restrictor.enforceRestrictions(root);
492 return restrictor.numErrors() == 0;
493}
494
Zhenyao Mo74da9f22013-09-23 14:57:01 -0400495void TCompiler::collectVariables(TIntermNode* root)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000496{
Jamie Madilld5512cd2014-07-10 17:50:08 -0400497 CollectVariables collect(&attributes,
498 &outputVariables,
499 &uniforms,
500 &varyings,
501 &interfaceBlocks,
502 hashFunction);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000503 root->traverse(&collect);
Jamie Madill23a8a432014-07-09 13:27:42 -0400504
505 // For backwards compatiblity with ShGetVariableInfo, expand struct
506 // uniforms and varyings into separate variables for each field.
507 ExpandVariables(uniforms, &expandedUniforms);
508 ExpandVariables(varyings, &expandedVaryings);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000509}
zmo@google.comfd747b82011-04-23 01:30:07 +0000510
gman@chromium.org8d804792012-10-17 21:33:48 +0000511bool TCompiler::enforcePackingRestrictions()
512{
513 VariablePacker packer;
Jamie Madill23a8a432014-07-09 13:27:42 -0400514 return packer.CheckVariablesWithinPackingLimits(maxUniformVectors, expandedUniforms);
gman@chromium.org8d804792012-10-17 21:33:48 +0000515}
516
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800517void TCompiler::initializeGLPosition(TIntermNode* root)
518{
519 InitializeVariables::InitVariableInfoList variables;
520 InitializeVariables::InitVariableInfo var(
521 "gl_Position", TType(EbtFloat, EbpUndefined, EvqPosition, 4));
522 variables.push_back(var);
523 InitializeVariables initializer(variables);
524 root->traverse(&initializer);
525}
526
527void TCompiler::initializeVaryingsWithoutStaticUse(TIntermNode* root)
528{
529 InitializeVariables::InitVariableInfoList variables;
530 for (size_t ii = 0; ii < varyings.size(); ++ii)
531 {
Jamie Madilla718c1e2014-07-02 15:31:22 -0400532 const sh::Varying& varying = varyings[ii];
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800533 if (varying.staticUse)
534 continue;
Jamie Madillaa72d782014-07-02 15:31:19 -0400535 unsigned char primarySize = static_cast<unsigned char>(gl::VariableColumnCount(varying.type));
536 unsigned char secondarySize = static_cast<unsigned char>(gl::VariableRowCount(varying.type));
Jamie Madilla718c1e2014-07-02 15:31:22 -0400537 TType type(EbtFloat, EbpUndefined, EvqVaryingOut, primarySize, secondarySize, varying.isArray());
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800538 TString name = varying.name.c_str();
Jamie Madilla718c1e2014-07-02 15:31:22 -0400539 if (varying.isArray())
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800540 {
Jamie Madilla718c1e2014-07-02 15:31:22 -0400541 type.setArraySize(varying.arraySize);
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800542 name = name.substr(0, name.find_first_of('['));
543 }
544
545 InitializeVariables::InitVariableInfo var(name, type);
546 variables.push_back(var);
547 }
548 InitializeVariables initializer(variables);
549 root->traverse(&initializer);
550}
551
zmo@google.com5601ea02011-06-10 18:23:25 +0000552const TExtensionBehavior& TCompiler::getExtensionBehavior() const
553{
554 return extensionBehavior;
555}
zmo@google.com32e97312011-08-24 01:03:11 +0000556
shannon.woods%transgaming.com@gtempaccount.com18b4c4b2013-04-13 03:31:40 +0000557const ShBuiltInResources& TCompiler::getResources() const
558{
559 return compileResources;
560}
561
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000562const ArrayBoundsClamper& TCompiler::getArrayBoundsClamper() const
563{
564 return arrayBoundsClamper;
565}
566
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000567ShArrayIndexClampingStrategy TCompiler::getArrayIndexClampingStrategy() const
568{
569 return clampingStrategy;
570}
571
572const BuiltInFunctionEmulator& TCompiler::getBuiltInFunctionEmulator() const
573{
574 return builtInFunctionEmulator;
575}