blob: 79bb283f8eb074738822ca5bc47d12175e09e0af [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"
Zhenyao Moe740add2014-07-18 17:01:01 -070015#include "compiler/translator/RegenerateStructNames.h"
Geoff Lang17732822013-08-29 13:46:49 -040016#include "compiler/translator/RenameFunction.h"
Zhenyao Mocd68fe72014-07-11 10:45:44 -070017#include "compiler/translator/ScalarizeVecAndMatConstructorArgs.h"
Zhenyao Mo7cab38b2013-10-15 12:59:30 -070018#include "compiler/translator/UnfoldShortCircuitAST.h"
Geoff Lang17732822013-08-29 13:46:49 -040019#include "compiler/translator/ValidateLimitations.h"
20#include "compiler/translator/ValidateOutputs.h"
21#include "compiler/translator/VariablePacker.h"
22#include "compiler/translator/depgraph/DependencyGraph.h"
23#include "compiler/translator/depgraph/DependencyGraphOutput.h"
24#include "compiler/translator/timing/RestrictFragmentShaderTiming.h"
25#include "compiler/translator/timing/RestrictVertexShaderTiming.h"
shannon.woods@transgaming.comda1ed362013-01-25 21:54:57 +000026#include "third_party/compiler/ArrayBoundsClamper.h"
Jamie Madill183bde52014-07-02 15:31:19 -040027#include "angle_gl.h"
Jamie Madillaa72d782014-07-02 15:31:19 -040028#include "common/utilities.h"
alokp@chromium.org07620a52010-09-23 17:53:56 +000029
Jamie Madill5508f392014-02-20 13:31:36 -050030bool IsWebGLBasedSpec(ShShaderSpec spec)
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +000031{
32 return spec == SH_WEBGL_SPEC || spec == SH_CSS_SHADERS_SPEC;
33}
34
Zhenyao Mo7faf1a12014-04-25 18:03:56 -070035size_t GetGlobalMaxTokenSize(ShShaderSpec spec)
Jamie Madill88f6e942014-02-19 10:27:53 -050036{
Jamie Madill88f6e942014-02-19 10:27:53 -050037 // WebGL defines a max token legnth of 256, while ES2 leaves max token
38 // size undefined. ES3 defines a max size of 1024 characters.
Zhenyao Mo7faf1a12014-04-25 18:03:56 -070039 if (IsWebGLBasedSpec(spec))
Jamie Madill88f6e942014-02-19 10:27:53 -050040 {
41 return 256;
42 }
43 else
44 {
45 return 1024;
46 }
47}
48
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +000049namespace {
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080050class TScopedPoolAllocator
51{
52 public:
53 TScopedPoolAllocator(TPoolAllocator* allocator) : mAllocator(allocator)
54 {
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -040055 mAllocator->push();
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +000056 SetGlobalPoolAllocator(mAllocator);
57 }
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080058 ~TScopedPoolAllocator()
59 {
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +000060 SetGlobalPoolAllocator(NULL);
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -040061 mAllocator->pop();
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +000062 }
63
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080064 private:
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +000065 TPoolAllocator* mAllocator;
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -040066};
67
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080068class TScopedSymbolTableLevel
69{
70 public:
71 TScopedSymbolTableLevel(TSymbolTable* table) : mTable(table)
72 {
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -040073 ASSERT(mTable->atBuiltInLevel());
74 mTable->push();
75 }
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080076 ~TScopedSymbolTableLevel()
77 {
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -040078 while (!mTable->atBuiltInLevel())
79 mTable->pop();
80 }
81
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080082 private:
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -040083 TSymbolTable* mTable;
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +000084};
85} // namespace
86
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080087TShHandleBase::TShHandleBase()
88{
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +000089 allocator.push();
90 SetGlobalPoolAllocator(&allocator);
91}
92
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080093TShHandleBase::~TShHandleBase()
94{
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +000095 SetGlobalPoolAllocator(NULL);
96 allocator.popAll();
97}
98
Jamie Madill183bde52014-07-02 15:31:19 -040099TCompiler::TCompiler(sh::GLenum type, ShShaderSpec spec, ShShaderOutput output)
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000100 : shaderType(type),
zmo@google.comf420c422011-09-12 18:27:59 +0000101 shaderSpec(spec),
Jamie Madill68fe74a2014-05-27 12:56:01 -0400102 outputType(output),
Jamie Madilleb1a0102013-07-08 13:31:38 -0400103 maxUniformVectors(0),
104 maxExpressionComplexity(0),
105 maxCallStackDepth(0),
shannon.woods%transgaming.com@gtempaccount.comcbb6b6a2013-04-13 03:27:47 +0000106 fragmentPrecisionHigh(false),
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000107 clampingStrategy(SH_CLAMP_WITH_CLAMP_INTRINSIC),
zmo@google.com9996b8e2012-01-19 01:43:55 +0000108 builtInFunctionEmulator(type)
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000109{
110}
111
112TCompiler::~TCompiler()
113{
114}
115
116bool TCompiler::Init(const ShBuiltInResources& resources)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000117{
shannon.woods%transgaming.com@gtempaccount.com0bbed382013-04-13 03:38:07 +0000118 shaderVersion = 100;
Jamie Madill183bde52014-07-02 15:31:19 -0400119 maxUniformVectors = (shaderType == GL_VERTEX_SHADER) ?
gman@chromium.org8d804792012-10-17 21:33:48 +0000120 resources.MaxVertexUniformVectors :
121 resources.MaxFragmentUniformVectors;
Jamie Madilleb1a0102013-07-08 13:31:38 -0400122 maxExpressionComplexity = resources.MaxExpressionComplexity;
123 maxCallStackDepth = resources.MaxCallStackDepth;
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400124
125 SetGlobalPoolAllocator(&allocator);
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000126
alokp@chromium.org07620a52010-09-23 17:53:56 +0000127 // Generate built-in symbol table.
128 if (!InitBuiltInSymbolTable(resources))
129 return false;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000130 InitExtensionBehavior(resources, extensionBehavior);
shannon.woods%transgaming.com@gtempaccount.comcbb6b6a2013-04-13 03:27:47 +0000131 fragmentPrecisionHigh = resources.FragmentPrecisionHigh == 1;
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000132
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000133 arrayBoundsClamper.SetClampingStrategy(resources.ArrayIndexClampingStrategy);
134 clampingStrategy = resources.ArrayIndexClampingStrategy;
135
daniel@transgaming.comc23f4612012-11-28 19:42:57 +0000136 hashFunction = resources.HashFunction;
137
alokp@chromium.org07620a52010-09-23 17:53:56 +0000138 return true;
139}
140
141bool TCompiler::compile(const char* const shaderStrings[],
shannon.woods@transgaming.comd64b3da2013-02-28 23:19:26 +0000142 size_t numStrings,
alokp@chromium.org07620a52010-09-23 17:53:56 +0000143 int compileOptions)
144{
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400145 TScopedPoolAllocator scopedAlloc(&allocator);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000146 clearResults();
147
148 if (numStrings == 0)
149 return true;
150
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000151 // If compiling for WebGL, validate loop and indexing as well.
Jamie Madill5508f392014-02-20 13:31:36 -0500152 if (IsWebGLBasedSpec(shaderSpec))
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000153 compileOptions |= SH_VALIDATE_LOOP_INDEXING;
alokp@chromium.org1f299542010-11-12 15:50:23 +0000154
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000155 // First string is path of source file if flag is set. The actual source follows.
156 const char* sourcePath = NULL;
shannon.woods@transgaming.comd64b3da2013-02-28 23:19:26 +0000157 size_t firstSource = 0;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000158 if (compileOptions & SH_SOURCE_PATH)
159 {
160 sourcePath = shaderStrings[0];
161 ++firstSource;
162 }
163
alokp@chromium.org07620a52010-09-23 17:53:56 +0000164 TIntermediate intermediate(infoSink);
165 TParseContext parseContext(symbolTable, extensionBehavior, intermediate,
zmo@google.comdc4b4f82011-06-17 00:42:53 +0000166 shaderType, shaderSpec, compileOptions, true,
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000167 sourcePath, infoSink);
shannon.woods%transgaming.com@gtempaccount.comcbb6b6a2013-04-13 03:27:47 +0000168 parseContext.fragmentPrecisionHigh = fragmentPrecisionHigh;
Alok Priyadarshi8156b6b2013-09-23 14:56:58 -0400169 SetGlobalParseContext(&parseContext);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000170
171 // We preserve symbols at the built-in level from compile-to-compile.
172 // Start pushing the user-defined symbols at global level.
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400173 TScopedSymbolTableLevel scopedSymbolLevel(&symbolTable);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000174
175 // Parse shader.
176 bool success =
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000177 (PaParseStrings(numStrings - firstSource, &shaderStrings[firstSource], NULL, &parseContext) == 0) &&
alokp@chromium.org07620a52010-09-23 17:53:56 +0000178 (parseContext.treeRoot != NULL);
shannon.woods%transgaming.com@gtempaccount.com0bbed382013-04-13 03:38:07 +0000179
shannon.woods%transgaming.com@gtempaccount.com5524db02013-04-13 03:38:16 +0000180 shaderVersion = parseContext.getShaderVersion();
shannon.woods%transgaming.com@gtempaccount.com0bbed382013-04-13 03:38:07 +0000181
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800182 if (success)
183 {
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000184 TIntermNode* root = parseContext.treeRoot;
185 success = intermediate.postProcess(root);
186
Jamie Madill6654bc92014-03-26 14:01:57 -0400187 // Disallow expressions deemed too complex.
188 if (success && (compileOptions & SH_LIMIT_EXPRESSION_COMPLEXITY))
189 success = limitExpressionComplexity(root);
190
zmo@google.comb1762df2011-07-30 02:04:23 +0000191 if (success)
Jamie Madilleb1a0102013-07-08 13:31:38 -0400192 success = detectCallDepth(root, infoSink, (compileOptions & SH_LIMIT_CALL_STACK_DEPTH) != 0);
zmo@google.comb1762df2011-07-30 02:04:23 +0000193
Jamie Madill183bde52014-07-02 15:31:19 -0400194 if (success && shaderVersion == 300 && shaderType == GL_FRAGMENT_SHADER)
Jamie Madill05a80ce2013-06-20 11:55:49 -0400195 success = validateOutputs(root);
196
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000197 if (success && (compileOptions & SH_VALIDATE_LOOP_INDEXING))
198 success = validateLimitations(root);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000199
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000200 if (success && (compileOptions & SH_TIMING_RESTRICTIONS))
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000201 success = enforceTimingRestrictions(root, (compileOptions & SH_DEPENDENCY_GRAPH) != 0);
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000202
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +0000203 if (success && shaderSpec == SH_CSS_SHADERS_SPEC)
204 rewriteCSSShader(root);
205
zmo@google.com0c6bb7a2011-08-17 19:39:58 +0000206 // Unroll for-loop markup needs to happen after validateLimitations pass.
207 if (success && (compileOptions & SH_UNROLL_FOR_LOOP_WITH_INTEGER_INDEX))
Zhenyao Mo3cdfcce2014-03-07 13:00:08 -0800208 {
209 ForLoopUnrollMarker marker(ForLoopUnrollMarker::kIntegerIndex);
Zhenyao Mo550c6002014-02-26 15:40:48 -0800210 root->traverse(&marker);
211 }
212 if (success && (compileOptions & SH_UNROLL_FOR_LOOP_WITH_SAMPLER_ARRAY_INDEX))
Zhenyao Mo3cdfcce2014-03-07 13:00:08 -0800213 {
214 ForLoopUnrollMarker marker(ForLoopUnrollMarker::kSamplerArrayIndex);
Zhenyao Mo550c6002014-02-26 15:40:48 -0800215 root->traverse(&marker);
216 if (marker.samplerArrayIndexIsFloatLoopIndex())
217 {
218 infoSink.info.prefix(EPrefixError);
219 infoSink.info << "sampler array index is float loop index";
220 success = false;
221 }
222 }
zmo@google.com0c6bb7a2011-08-17 19:39:58 +0000223
zmo@google.com32e97312011-08-24 01:03:11 +0000224 // Built-in function emulation needs to happen after validateLimitations pass.
225 if (success && (compileOptions & SH_EMULATE_BUILT_IN_FUNCTIONS))
226 builtInFunctionEmulator.MarkBuiltInFunctionsForEmulation(root);
227
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000228 // Clamping uniform array bounds needs to happen after validateLimitations pass.
229 if (success && (compileOptions & SH_CLAMP_INDIRECT_ARRAY_BOUNDS))
230 arrayBoundsClamper.MarkIndirectArrayBoundsForClamping(root);
231
Jamie Madill183bde52014-07-02 15:31:19 -0400232 if (success && shaderType == GL_VERTEX_SHADER && (compileOptions & SH_INIT_GL_POSITION))
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800233 initializeGLPosition(root);
Zhenyao Moac44cd22013-09-23 14:57:09 -0400234
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800235 if (success && (compileOptions & SH_UNFOLD_SHORT_CIRCUIT))
236 {
Zhenyao Mo7cab38b2013-10-15 12:59:30 -0700237 UnfoldShortCircuitAST unfoldShortCircuit;
238 root->traverse(&unfoldShortCircuit);
239 unfoldShortCircuit.updateTree();
240 }
241
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800242 if (success && (compileOptions & SH_VARIABLES))
243 {
Zhenyao Mo74da9f22013-09-23 14:57:01 -0400244 collectVariables(root);
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800245 if (compileOptions & SH_ENFORCE_PACKING_RESTRICTIONS)
246 {
gman@chromium.org8d804792012-10-17 21:33:48 +0000247 success = enforcePackingRestrictions();
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800248 if (!success)
249 {
Jamie Madill075edd82013-07-08 13:30:19 -0400250 infoSink.info.prefix(EPrefixError);
251 infoSink.info << "too many uniforms";
gman@chromium.org8d804792012-10-17 21:33:48 +0000252 }
253 }
Jamie Madill183bde52014-07-02 15:31:19 -0400254 if (success && shaderType == GL_VERTEX_SHADER &&
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800255 (compileOptions & SH_INIT_VARYINGS_WITHOUT_STATIC_USE))
256 initializeVaryingsWithoutStaticUse(root);
gman@chromium.org8d804792012-10-17 21:33:48 +0000257 }
zmo@google.comfd747b82011-04-23 01:30:07 +0000258
Zhenyao Mocd68fe72014-07-11 10:45:44 -0700259 if (success && (compileOptions & SH_SCALARIZE_VEC_AND_MAT_CONSTRUCTOR_ARGS))
260 {
261 ScalarizeVecAndMatConstructorArgs scalarizer;
262 root->traverse(&scalarizer);
263 }
264
Zhenyao Moe740add2014-07-18 17:01:01 -0700265 if (success && (compileOptions & SH_REGENERATE_STRUCT_NAMES))
266 {
267 RegenerateStructNames gen(symbolTable, shaderVersion);
268 root->traverse(&gen);
269 }
270
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000271 if (success && (compileOptions & SH_INTERMEDIATE_TREE))
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000272 intermediate.outputTree(root);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000273
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000274 if (success && (compileOptions & SH_OBJECT_CODE))
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000275 translate(root);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000276 }
277
278 // Cleanup memory.
279 intermediate.remove(parseContext.treeRoot);
Zhenyao Mo7faf1a12014-04-25 18:03:56 -0700280 SetGlobalParseContext(NULL);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000281 return success;
282}
283
Nicolas Capens49a88872013-06-20 09:54:03 -0400284bool TCompiler::InitBuiltInSymbolTable(const ShBuiltInResources &resources)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000285{
shannon.woods%transgaming.com@gtempaccount.com18b4c4b2013-04-13 03:31:40 +0000286 compileResources = resources;
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400287 setResourceString();
shannonwoods@chromium.org2ac0be92013-05-30 00:02:27 +0000288
Nicolas Capens49a88872013-06-20 09:54:03 -0400289 assert(symbolTable.isEmpty());
290 symbolTable.push(); // COMMON_BUILTINS
291 symbolTable.push(); // ESSL1_BUILTINS
292 symbolTable.push(); // ESSL3_BUILTINS
shannonwoods@chromium.org2ac0be92013-05-30 00:02:27 +0000293
Nicolas Capens49a88872013-06-20 09:54:03 -0400294 TPublicType integer;
295 integer.type = EbtInt;
296 integer.primarySize = 1;
297 integer.secondarySize = 1;
298 integer.array = false;
299
300 TPublicType floatingPoint;
301 floatingPoint.type = EbtFloat;
302 floatingPoint.primarySize = 1;
303 floatingPoint.secondarySize = 1;
304 floatingPoint.array = false;
305
Zhenyao Moa5a1dfc2013-09-23 14:57:03 -0400306 TPublicType sampler;
307 sampler.primarySize = 1;
308 sampler.secondarySize = 1;
309 sampler.array = false;
310
Nicolas Capens49a88872013-06-20 09:54:03 -0400311 switch(shaderType)
312 {
Jamie Madill183bde52014-07-02 15:31:19 -0400313 case GL_FRAGMENT_SHADER:
Nicolas Capens49a88872013-06-20 09:54:03 -0400314 symbolTable.setDefaultPrecision(integer, EbpMedium);
315 break;
Jamie Madill183bde52014-07-02 15:31:19 -0400316 case GL_VERTEX_SHADER:
Nicolas Capens49a88872013-06-20 09:54:03 -0400317 symbolTable.setDefaultPrecision(integer, EbpHigh);
318 symbolTable.setDefaultPrecision(floatingPoint, EbpHigh);
319 break;
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800320 default:
321 assert(false && "Language not supported");
Nicolas Capens49a88872013-06-20 09:54:03 -0400322 }
Zhenyao Moa5a1dfc2013-09-23 14:57:03 -0400323 // We set defaults for all the sampler types, even those that are
324 // only available if an extension exists.
325 for (int samplerType = EbtGuardSamplerBegin + 1;
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800326 samplerType < EbtGuardSamplerEnd; ++samplerType)
327 {
Zhenyao Moa5a1dfc2013-09-23 14:57:03 -0400328 sampler.type = static_cast<TBasicType>(samplerType);
329 symbolTable.setDefaultPrecision(sampler, EbpLow);
330 }
Nicolas Capens49a88872013-06-20 09:54:03 -0400331
Jamie Madill1b452142013-07-12 14:51:11 -0400332 InsertBuiltInFunctions(shaderType, shaderSpec, resources, symbolTable);
Nicolas Capens49a88872013-06-20 09:54:03 -0400333
334 IdentifyBuiltIns(shaderType, shaderSpec, resources, symbolTable);
335
336 return true;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000337}
338
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400339void TCompiler::setResourceString()
340{
341 std::ostringstream strstream;
342 strstream << ":MaxVertexAttribs:" << compileResources.MaxVertexAttribs
343 << ":MaxVertexUniformVectors:" << compileResources.MaxVertexUniformVectors
344 << ":MaxVaryingVectors:" << compileResources.MaxVaryingVectors
345 << ":MaxVertexTextureImageUnits:" << compileResources.MaxVertexTextureImageUnits
346 << ":MaxCombinedTextureImageUnits:" << compileResources.MaxCombinedTextureImageUnits
347 << ":MaxTextureImageUnits:" << compileResources.MaxTextureImageUnits
348 << ":MaxFragmentUniformVectors:" << compileResources.MaxFragmentUniformVectors
349 << ":MaxDrawBuffers:" << compileResources.MaxDrawBuffers
350 << ":OES_standard_derivatives:" << compileResources.OES_standard_derivatives
351 << ":OES_EGL_image_external:" << compileResources.OES_EGL_image_external
352 << ":ARB_texture_rectangle:" << compileResources.ARB_texture_rectangle
353 << ":EXT_draw_buffers:" << compileResources.EXT_draw_buffers
354 << ":FragmentPrecisionHigh:" << compileResources.FragmentPrecisionHigh
355 << ":MaxExpressionComplexity:" << compileResources.MaxExpressionComplexity
356 << ":MaxCallStackDepth:" << compileResources.MaxCallStackDepth
357 << ":EXT_frag_depth:" << compileResources.EXT_frag_depth
358 << ":EXT_shader_texture_lod:" << compileResources.EXT_shader_texture_lod
359 << ":MaxVertexOutputVectors:" << compileResources.MaxVertexOutputVectors
360 << ":MaxFragmentInputVectors:" << compileResources.MaxFragmentInputVectors
361 << ":MinProgramTexelOffset:" << compileResources.MinProgramTexelOffset
362 << ":MaxProgramTexelOffset:" << compileResources.MaxProgramTexelOffset;
363
364 builtInResourcesString = strstream.str();
365}
366
alokp@chromium.org07620a52010-09-23 17:53:56 +0000367void TCompiler::clearResults()
368{
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000369 arrayBoundsClamper.Cleanup();
alokp@chromium.org07620a52010-09-23 17:53:56 +0000370 infoSink.info.erase();
371 infoSink.obj.erase();
372 infoSink.debug.erase();
373
Jamie Madilled27c722014-07-02 15:31:23 -0400374 attributes.clear();
375 outputVariables.clear();
alokp@chromium.org07620a52010-09-23 17:53:56 +0000376 uniforms.clear();
Jamie Madill23a8a432014-07-09 13:27:42 -0400377 expandedUniforms.clear();
Zhenyao Mod2d340b2013-09-23 14:57:05 -0400378 varyings.clear();
Jamie Madill23a8a432014-07-09 13:27:42 -0400379 expandedVaryings.clear();
Jamie Madilled27c722014-07-02 15:31:23 -0400380 interfaceBlocks.clear();
zmo@google.coma3b4ab42011-09-16 00:53:26 +0000381
382 builtInFunctionEmulator.Cleanup();
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +0000383
384 nameMap.clear();
alokp@chromium.org07620a52010-09-23 17:53:56 +0000385}
386
Jamie Madilleb1a0102013-07-08 13:31:38 -0400387bool TCompiler::detectCallDepth(TIntermNode* root, TInfoSink& infoSink, bool limitCallStackDepth)
zmo@google.comb1762df2011-07-30 02:04:23 +0000388{
Jamie Madilleb1a0102013-07-08 13:31:38 -0400389 DetectCallDepth detect(infoSink, limitCallStackDepth, maxCallStackDepth);
zmo@google.comb1762df2011-07-30 02:04:23 +0000390 root->traverse(&detect);
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800391 switch (detect.detectCallDepth())
392 {
393 case DetectCallDepth::kErrorNone:
394 return true;
395 case DetectCallDepth::kErrorMissingMain:
396 infoSink.info.prefix(EPrefixError);
397 infoSink.info << "Missing main()";
398 return false;
399 case DetectCallDepth::kErrorRecursion:
400 infoSink.info.prefix(EPrefixError);
401 infoSink.info << "Function recursion detected";
402 return false;
403 case DetectCallDepth::kErrorMaxDepthExceeded:
404 infoSink.info.prefix(EPrefixError);
405 infoSink.info << "Function call stack too deep";
406 return false;
407 default:
408 UNREACHABLE();
409 return false;
zmo@google.comb1762df2011-07-30 02:04:23 +0000410 }
411}
412
Jamie Madill05a80ce2013-06-20 11:55:49 -0400413bool TCompiler::validateOutputs(TIntermNode* root)
414{
415 ValidateOutputs validateOutputs(infoSink.info, compileResources.MaxDrawBuffers);
416 root->traverse(&validateOutputs);
417 return (validateOutputs.numErrors() == 0);
418}
419
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +0000420void TCompiler::rewriteCSSShader(TIntermNode* root)
421{
422 RenameFunction renamer("main(", "css_main(");
423 root->traverse(&renamer);
424}
425
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800426bool TCompiler::validateLimitations(TIntermNode* root)
427{
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000428 ValidateLimitations validate(shaderType, infoSink.info);
429 root->traverse(&validate);
430 return validate.numErrors() == 0;
431}
432
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000433bool TCompiler::enforceTimingRestrictions(TIntermNode* root, bool outputGraph)
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000434{
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800435 if (shaderSpec != SH_WEBGL_SPEC)
436 {
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000437 infoSink.info << "Timing restrictions must be enforced under the WebGL spec.";
438 return false;
439 }
440
Jamie Madill183bde52014-07-02 15:31:19 -0400441 if (shaderType == GL_FRAGMENT_SHADER)
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800442 {
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000443 TDependencyGraph graph(root);
444
445 // Output any errors first.
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000446 bool success = enforceFragmentShaderTimingRestrictions(graph);
Jamie Madill5508f392014-02-20 13:31:36 -0500447
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000448 // Then, output the dependency graph.
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800449 if (outputGraph)
450 {
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000451 TDependencyGraphOutput output(infoSink.info);
452 output.outputAllSpanningTrees(graph);
453 }
Jamie Madill5508f392014-02-20 13:31:36 -0500454
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000455 return success;
456 }
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800457 else
458 {
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000459 return enforceVertexShaderTimingRestrictions(root);
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000460 }
461}
462
Jamie Madilleb1a0102013-07-08 13:31:38 -0400463bool TCompiler::limitExpressionComplexity(TIntermNode* root)
464{
Jamie Madill6654bc92014-03-26 14:01:57 -0400465 TMaxDepthTraverser traverser(maxExpressionComplexity+1);
Jamie Madilleb1a0102013-07-08 13:31:38 -0400466 root->traverse(&traverser);
Jamie Madill6654bc92014-03-26 14:01:57 -0400467
468 if (traverser.getMaxDepth() > maxExpressionComplexity)
469 {
470 infoSink.info << "Expression too complex.";
471 return false;
472 }
473
Jamie Madilleb1a0102013-07-08 13:31:38 -0400474 TDependencyGraph graph(root);
475
476 for (TFunctionCallVector::const_iterator iter = graph.beginUserDefinedFunctionCalls();
477 iter != graph.endUserDefinedFunctionCalls();
478 ++iter)
479 {
480 TGraphFunctionCall* samplerSymbol = *iter;
481 TDependencyGraphTraverser graphTraverser;
482 samplerSymbol->traverse(&graphTraverser);
483 }
484
Jamie Madilleb1a0102013-07-08 13:31:38 -0400485 return true;
486}
487
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000488bool TCompiler::enforceFragmentShaderTimingRestrictions(const TDependencyGraph& graph)
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000489{
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000490 RestrictFragmentShaderTiming restrictor(infoSink.info);
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000491 restrictor.enforceRestrictions(graph);
492 return restrictor.numErrors() == 0;
493}
494
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000495bool TCompiler::enforceVertexShaderTimingRestrictions(TIntermNode* root)
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000496{
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000497 RestrictVertexShaderTiming restrictor(infoSink.info);
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000498 restrictor.enforceRestrictions(root);
499 return restrictor.numErrors() == 0;
500}
501
Zhenyao Mo74da9f22013-09-23 14:57:01 -0400502void TCompiler::collectVariables(TIntermNode* root)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000503{
Jamie Madilld5512cd2014-07-10 17:50:08 -0400504 CollectVariables collect(&attributes,
505 &outputVariables,
506 &uniforms,
507 &varyings,
508 &interfaceBlocks,
509 hashFunction);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000510 root->traverse(&collect);
Jamie Madill23a8a432014-07-09 13:27:42 -0400511
512 // For backwards compatiblity with ShGetVariableInfo, expand struct
513 // uniforms and varyings into separate variables for each field.
514 ExpandVariables(uniforms, &expandedUniforms);
515 ExpandVariables(varyings, &expandedVaryings);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000516}
zmo@google.comfd747b82011-04-23 01:30:07 +0000517
gman@chromium.org8d804792012-10-17 21:33:48 +0000518bool TCompiler::enforcePackingRestrictions()
519{
520 VariablePacker packer;
Jamie Madill23a8a432014-07-09 13:27:42 -0400521 return packer.CheckVariablesWithinPackingLimits(maxUniformVectors, expandedUniforms);
gman@chromium.org8d804792012-10-17 21:33:48 +0000522}
523
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800524void TCompiler::initializeGLPosition(TIntermNode* root)
525{
526 InitializeVariables::InitVariableInfoList variables;
527 InitializeVariables::InitVariableInfo var(
528 "gl_Position", TType(EbtFloat, EbpUndefined, EvqPosition, 4));
529 variables.push_back(var);
530 InitializeVariables initializer(variables);
531 root->traverse(&initializer);
532}
533
534void TCompiler::initializeVaryingsWithoutStaticUse(TIntermNode* root)
535{
536 InitializeVariables::InitVariableInfoList variables;
537 for (size_t ii = 0; ii < varyings.size(); ++ii)
538 {
Jamie Madilla718c1e2014-07-02 15:31:22 -0400539 const sh::Varying& varying = varyings[ii];
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800540 if (varying.staticUse)
541 continue;
Jamie Madillaa72d782014-07-02 15:31:19 -0400542 unsigned char primarySize = static_cast<unsigned char>(gl::VariableColumnCount(varying.type));
543 unsigned char secondarySize = static_cast<unsigned char>(gl::VariableRowCount(varying.type));
Jamie Madilla718c1e2014-07-02 15:31:22 -0400544 TType type(EbtFloat, EbpUndefined, EvqVaryingOut, primarySize, secondarySize, varying.isArray());
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800545 TString name = varying.name.c_str();
Jamie Madilla718c1e2014-07-02 15:31:22 -0400546 if (varying.isArray())
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800547 {
Jamie Madilla718c1e2014-07-02 15:31:22 -0400548 type.setArraySize(varying.arraySize);
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800549 name = name.substr(0, name.find_first_of('['));
550 }
551
552 InitializeVariables::InitVariableInfo var(name, type);
553 variables.push_back(var);
554 }
555 InitializeVariables initializer(variables);
556 root->traverse(&initializer);
557}
558
zmo@google.com5601ea02011-06-10 18:23:25 +0000559const TExtensionBehavior& TCompiler::getExtensionBehavior() const
560{
561 return extensionBehavior;
562}
zmo@google.com32e97312011-08-24 01:03:11 +0000563
shannon.woods%transgaming.com@gtempaccount.com18b4c4b2013-04-13 03:31:40 +0000564const ShBuiltInResources& TCompiler::getResources() const
565{
566 return compileResources;
567}
568
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000569const ArrayBoundsClamper& TCompiler::getArrayBoundsClamper() const
570{
571 return arrayBoundsClamper;
572}
573
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000574ShArrayIndexClampingStrategy TCompiler::getArrayIndexClampingStrategy() const
575{
576 return clampingStrategy;
577}
578
579const BuiltInFunctionEmulator& TCompiler::getBuiltInFunctionEmulator() const
580{
581 return builtInFunctionEmulator;
582}