blob: bcb431a6229dd60b2ec07cc61440b9be92a36aa7 [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 {
Zhenyao Modaf56572014-08-06 16:18:30 -0700261 ScalarizeVecAndMatConstructorArgs scalarizer(
262 shaderType, fragmentPrecisionHigh);
Zhenyao Mocd68fe72014-07-11 10:45:44 -0700263 root->traverse(&scalarizer);
264 }
265
Zhenyao Moe740add2014-07-18 17:01:01 -0700266 if (success && (compileOptions & SH_REGENERATE_STRUCT_NAMES))
267 {
268 RegenerateStructNames gen(symbolTable, shaderVersion);
269 root->traverse(&gen);
270 }
271
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000272 if (success && (compileOptions & SH_INTERMEDIATE_TREE))
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000273 intermediate.outputTree(root);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000274
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000275 if (success && (compileOptions & SH_OBJECT_CODE))
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000276 translate(root);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000277 }
278
279 // Cleanup memory.
280 intermediate.remove(parseContext.treeRoot);
Zhenyao Mo7faf1a12014-04-25 18:03:56 -0700281 SetGlobalParseContext(NULL);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000282 return success;
283}
284
Nicolas Capens49a88872013-06-20 09:54:03 -0400285bool TCompiler::InitBuiltInSymbolTable(const ShBuiltInResources &resources)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000286{
shannon.woods%transgaming.com@gtempaccount.com18b4c4b2013-04-13 03:31:40 +0000287 compileResources = resources;
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400288 setResourceString();
shannonwoods@chromium.org2ac0be92013-05-30 00:02:27 +0000289
Nicolas Capens49a88872013-06-20 09:54:03 -0400290 assert(symbolTable.isEmpty());
291 symbolTable.push(); // COMMON_BUILTINS
292 symbolTable.push(); // ESSL1_BUILTINS
293 symbolTable.push(); // ESSL3_BUILTINS
shannonwoods@chromium.org2ac0be92013-05-30 00:02:27 +0000294
Nicolas Capens49a88872013-06-20 09:54:03 -0400295 TPublicType integer;
296 integer.type = EbtInt;
297 integer.primarySize = 1;
298 integer.secondarySize = 1;
299 integer.array = false;
300
301 TPublicType floatingPoint;
302 floatingPoint.type = EbtFloat;
303 floatingPoint.primarySize = 1;
304 floatingPoint.secondarySize = 1;
305 floatingPoint.array = false;
306
Zhenyao Moa5a1dfc2013-09-23 14:57:03 -0400307 TPublicType sampler;
308 sampler.primarySize = 1;
309 sampler.secondarySize = 1;
310 sampler.array = false;
311
Nicolas Capens49a88872013-06-20 09:54:03 -0400312 switch(shaderType)
313 {
Jamie Madill183bde52014-07-02 15:31:19 -0400314 case GL_FRAGMENT_SHADER:
Nicolas Capens49a88872013-06-20 09:54:03 -0400315 symbolTable.setDefaultPrecision(integer, EbpMedium);
316 break;
Jamie Madill183bde52014-07-02 15:31:19 -0400317 case GL_VERTEX_SHADER:
Nicolas Capens49a88872013-06-20 09:54:03 -0400318 symbolTable.setDefaultPrecision(integer, EbpHigh);
319 symbolTable.setDefaultPrecision(floatingPoint, EbpHigh);
320 break;
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800321 default:
322 assert(false && "Language not supported");
Nicolas Capens49a88872013-06-20 09:54:03 -0400323 }
Zhenyao Moa5a1dfc2013-09-23 14:57:03 -0400324 // We set defaults for all the sampler types, even those that are
325 // only available if an extension exists.
326 for (int samplerType = EbtGuardSamplerBegin + 1;
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800327 samplerType < EbtGuardSamplerEnd; ++samplerType)
328 {
Zhenyao Moa5a1dfc2013-09-23 14:57:03 -0400329 sampler.type = static_cast<TBasicType>(samplerType);
330 symbolTable.setDefaultPrecision(sampler, EbpLow);
331 }
Nicolas Capens49a88872013-06-20 09:54:03 -0400332
Jamie Madill1b452142013-07-12 14:51:11 -0400333 InsertBuiltInFunctions(shaderType, shaderSpec, resources, symbolTable);
Nicolas Capens49a88872013-06-20 09:54:03 -0400334
335 IdentifyBuiltIns(shaderType, shaderSpec, resources, symbolTable);
336
337 return true;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000338}
339
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400340void TCompiler::setResourceString()
341{
342 std::ostringstream strstream;
343 strstream << ":MaxVertexAttribs:" << compileResources.MaxVertexAttribs
344 << ":MaxVertexUniformVectors:" << compileResources.MaxVertexUniformVectors
345 << ":MaxVaryingVectors:" << compileResources.MaxVaryingVectors
346 << ":MaxVertexTextureImageUnits:" << compileResources.MaxVertexTextureImageUnits
347 << ":MaxCombinedTextureImageUnits:" << compileResources.MaxCombinedTextureImageUnits
348 << ":MaxTextureImageUnits:" << compileResources.MaxTextureImageUnits
349 << ":MaxFragmentUniformVectors:" << compileResources.MaxFragmentUniformVectors
350 << ":MaxDrawBuffers:" << compileResources.MaxDrawBuffers
351 << ":OES_standard_derivatives:" << compileResources.OES_standard_derivatives
352 << ":OES_EGL_image_external:" << compileResources.OES_EGL_image_external
353 << ":ARB_texture_rectangle:" << compileResources.ARB_texture_rectangle
354 << ":EXT_draw_buffers:" << compileResources.EXT_draw_buffers
355 << ":FragmentPrecisionHigh:" << compileResources.FragmentPrecisionHigh
356 << ":MaxExpressionComplexity:" << compileResources.MaxExpressionComplexity
357 << ":MaxCallStackDepth:" << compileResources.MaxCallStackDepth
358 << ":EXT_frag_depth:" << compileResources.EXT_frag_depth
359 << ":EXT_shader_texture_lod:" << compileResources.EXT_shader_texture_lod
360 << ":MaxVertexOutputVectors:" << compileResources.MaxVertexOutputVectors
361 << ":MaxFragmentInputVectors:" << compileResources.MaxFragmentInputVectors
362 << ":MinProgramTexelOffset:" << compileResources.MinProgramTexelOffset
363 << ":MaxProgramTexelOffset:" << compileResources.MaxProgramTexelOffset;
364
365 builtInResourcesString = strstream.str();
366}
367
alokp@chromium.org07620a52010-09-23 17:53:56 +0000368void TCompiler::clearResults()
369{
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000370 arrayBoundsClamper.Cleanup();
alokp@chromium.org07620a52010-09-23 17:53:56 +0000371 infoSink.info.erase();
372 infoSink.obj.erase();
373 infoSink.debug.erase();
374
Jamie Madilled27c722014-07-02 15:31:23 -0400375 attributes.clear();
376 outputVariables.clear();
alokp@chromium.org07620a52010-09-23 17:53:56 +0000377 uniforms.clear();
Jamie Madill23a8a432014-07-09 13:27:42 -0400378 expandedUniforms.clear();
Zhenyao Mod2d340b2013-09-23 14:57:05 -0400379 varyings.clear();
Jamie Madill23a8a432014-07-09 13:27:42 -0400380 expandedVaryings.clear();
Jamie Madilled27c722014-07-02 15:31:23 -0400381 interfaceBlocks.clear();
zmo@google.coma3b4ab42011-09-16 00:53:26 +0000382
383 builtInFunctionEmulator.Cleanup();
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +0000384
385 nameMap.clear();
alokp@chromium.org07620a52010-09-23 17:53:56 +0000386}
387
Jamie Madilleb1a0102013-07-08 13:31:38 -0400388bool TCompiler::detectCallDepth(TIntermNode* root, TInfoSink& infoSink, bool limitCallStackDepth)
zmo@google.comb1762df2011-07-30 02:04:23 +0000389{
Jamie Madilleb1a0102013-07-08 13:31:38 -0400390 DetectCallDepth detect(infoSink, limitCallStackDepth, maxCallStackDepth);
zmo@google.comb1762df2011-07-30 02:04:23 +0000391 root->traverse(&detect);
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800392 switch (detect.detectCallDepth())
393 {
394 case DetectCallDepth::kErrorNone:
395 return true;
396 case DetectCallDepth::kErrorMissingMain:
397 infoSink.info.prefix(EPrefixError);
398 infoSink.info << "Missing main()";
399 return false;
400 case DetectCallDepth::kErrorRecursion:
401 infoSink.info.prefix(EPrefixError);
402 infoSink.info << "Function recursion detected";
403 return false;
404 case DetectCallDepth::kErrorMaxDepthExceeded:
405 infoSink.info.prefix(EPrefixError);
406 infoSink.info << "Function call stack too deep";
407 return false;
408 default:
409 UNREACHABLE();
410 return false;
zmo@google.comb1762df2011-07-30 02:04:23 +0000411 }
412}
413
Jamie Madill05a80ce2013-06-20 11:55:49 -0400414bool TCompiler::validateOutputs(TIntermNode* root)
415{
416 ValidateOutputs validateOutputs(infoSink.info, compileResources.MaxDrawBuffers);
417 root->traverse(&validateOutputs);
418 return (validateOutputs.numErrors() == 0);
419}
420
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +0000421void TCompiler::rewriteCSSShader(TIntermNode* root)
422{
423 RenameFunction renamer("main(", "css_main(");
424 root->traverse(&renamer);
425}
426
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800427bool TCompiler::validateLimitations(TIntermNode* root)
428{
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000429 ValidateLimitations validate(shaderType, infoSink.info);
430 root->traverse(&validate);
431 return validate.numErrors() == 0;
432}
433
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000434bool TCompiler::enforceTimingRestrictions(TIntermNode* root, bool outputGraph)
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000435{
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800436 if (shaderSpec != SH_WEBGL_SPEC)
437 {
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000438 infoSink.info << "Timing restrictions must be enforced under the WebGL spec.";
439 return false;
440 }
441
Jamie Madill183bde52014-07-02 15:31:19 -0400442 if (shaderType == GL_FRAGMENT_SHADER)
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800443 {
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000444 TDependencyGraph graph(root);
445
446 // Output any errors first.
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000447 bool success = enforceFragmentShaderTimingRestrictions(graph);
Jamie Madill5508f392014-02-20 13:31:36 -0500448
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000449 // Then, output the dependency graph.
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800450 if (outputGraph)
451 {
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000452 TDependencyGraphOutput output(infoSink.info);
453 output.outputAllSpanningTrees(graph);
454 }
Jamie Madill5508f392014-02-20 13:31:36 -0500455
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000456 return success;
457 }
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800458 else
459 {
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000460 return enforceVertexShaderTimingRestrictions(root);
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000461 }
462}
463
Jamie Madilleb1a0102013-07-08 13:31:38 -0400464bool TCompiler::limitExpressionComplexity(TIntermNode* root)
465{
Jamie Madill6654bc92014-03-26 14:01:57 -0400466 TMaxDepthTraverser traverser(maxExpressionComplexity+1);
Jamie Madilleb1a0102013-07-08 13:31:38 -0400467 root->traverse(&traverser);
Jamie Madill6654bc92014-03-26 14:01:57 -0400468
469 if (traverser.getMaxDepth() > maxExpressionComplexity)
470 {
471 infoSink.info << "Expression too complex.";
472 return false;
473 }
474
Jamie Madilleb1a0102013-07-08 13:31:38 -0400475 TDependencyGraph graph(root);
476
477 for (TFunctionCallVector::const_iterator iter = graph.beginUserDefinedFunctionCalls();
478 iter != graph.endUserDefinedFunctionCalls();
479 ++iter)
480 {
481 TGraphFunctionCall* samplerSymbol = *iter;
482 TDependencyGraphTraverser graphTraverser;
483 samplerSymbol->traverse(&graphTraverser);
484 }
485
Jamie Madilleb1a0102013-07-08 13:31:38 -0400486 return true;
487}
488
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000489bool TCompiler::enforceFragmentShaderTimingRestrictions(const TDependencyGraph& graph)
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000490{
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000491 RestrictFragmentShaderTiming restrictor(infoSink.info);
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000492 restrictor.enforceRestrictions(graph);
493 return restrictor.numErrors() == 0;
494}
495
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000496bool TCompiler::enforceVertexShaderTimingRestrictions(TIntermNode* root)
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000497{
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000498 RestrictVertexShaderTiming restrictor(infoSink.info);
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000499 restrictor.enforceRestrictions(root);
500 return restrictor.numErrors() == 0;
501}
502
Zhenyao Mo74da9f22013-09-23 14:57:01 -0400503void TCompiler::collectVariables(TIntermNode* root)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000504{
Jamie Madilld5512cd2014-07-10 17:50:08 -0400505 CollectVariables collect(&attributes,
506 &outputVariables,
507 &uniforms,
508 &varyings,
509 &interfaceBlocks,
510 hashFunction);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000511 root->traverse(&collect);
Jamie Madill23a8a432014-07-09 13:27:42 -0400512
513 // For backwards compatiblity with ShGetVariableInfo, expand struct
514 // uniforms and varyings into separate variables for each field.
515 ExpandVariables(uniforms, &expandedUniforms);
516 ExpandVariables(varyings, &expandedVaryings);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000517}
zmo@google.comfd747b82011-04-23 01:30:07 +0000518
gman@chromium.org8d804792012-10-17 21:33:48 +0000519bool TCompiler::enforcePackingRestrictions()
520{
521 VariablePacker packer;
Jamie Madill23a8a432014-07-09 13:27:42 -0400522 return packer.CheckVariablesWithinPackingLimits(maxUniformVectors, expandedUniforms);
gman@chromium.org8d804792012-10-17 21:33:48 +0000523}
524
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800525void TCompiler::initializeGLPosition(TIntermNode* root)
526{
527 InitializeVariables::InitVariableInfoList variables;
528 InitializeVariables::InitVariableInfo var(
529 "gl_Position", TType(EbtFloat, EbpUndefined, EvqPosition, 4));
530 variables.push_back(var);
531 InitializeVariables initializer(variables);
532 root->traverse(&initializer);
533}
534
535void TCompiler::initializeVaryingsWithoutStaticUse(TIntermNode* root)
536{
537 InitializeVariables::InitVariableInfoList variables;
538 for (size_t ii = 0; ii < varyings.size(); ++ii)
539 {
Jamie Madilla718c1e2014-07-02 15:31:22 -0400540 const sh::Varying& varying = varyings[ii];
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800541 if (varying.staticUse)
542 continue;
Jamie Madillaa72d782014-07-02 15:31:19 -0400543 unsigned char primarySize = static_cast<unsigned char>(gl::VariableColumnCount(varying.type));
544 unsigned char secondarySize = static_cast<unsigned char>(gl::VariableRowCount(varying.type));
Jamie Madilla718c1e2014-07-02 15:31:22 -0400545 TType type(EbtFloat, EbpUndefined, EvqVaryingOut, primarySize, secondarySize, varying.isArray());
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800546 TString name = varying.name.c_str();
Jamie Madilla718c1e2014-07-02 15:31:22 -0400547 if (varying.isArray())
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800548 {
Jamie Madilla718c1e2014-07-02 15:31:22 -0400549 type.setArraySize(varying.arraySize);
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800550 name = name.substr(0, name.find_first_of('['));
551 }
552
553 InitializeVariables::InitVariableInfo var(name, type);
554 variables.push_back(var);
555 }
556 InitializeVariables initializer(variables);
557 root->traverse(&initializer);
558}
559
zmo@google.com5601ea02011-06-10 18:23:25 +0000560const TExtensionBehavior& TCompiler::getExtensionBehavior() const
561{
562 return extensionBehavior;
563}
zmo@google.com32e97312011-08-24 01:03:11 +0000564
shannon.woods%transgaming.com@gtempaccount.com18b4c4b2013-04-13 03:31:40 +0000565const ShBuiltInResources& TCompiler::getResources() const
566{
567 return compileResources;
568}
569
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000570const ArrayBoundsClamper& TCompiler::getArrayBoundsClamper() const
571{
572 return arrayBoundsClamper;
573}
574
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000575ShArrayIndexClampingStrategy TCompiler::getArrayIndexClampingStrategy() const
576{
577 return clampingStrategy;
578}
579
580const BuiltInFunctionEmulator& TCompiler::getBuiltInFunctionEmulator() const
581{
582 return builtInFunctionEmulator;
583}