blob: 5c62a64d10ba4a67ec5f7f450278b401c3ffe400 [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{
Zhenyao Modb9b40b2014-10-29 15:00:04 -070032 return (spec == SH_WEBGL_SPEC ||
33 spec == SH_CSS_SHADERS_SPEC ||
34 spec == SH_WEBGL2_SPEC);
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +000035}
36
Zhenyao Mo7faf1a12014-04-25 18:03:56 -070037size_t GetGlobalMaxTokenSize(ShShaderSpec spec)
Jamie Madill88f6e942014-02-19 10:27:53 -050038{
Jamie Madill88f6e942014-02-19 10:27:53 -050039 // WebGL defines a max token legnth of 256, while ES2 leaves max token
40 // size undefined. ES3 defines a max size of 1024 characters.
Zhenyao Modb9b40b2014-10-29 15:00:04 -070041 switch (spec)
Jamie Madill88f6e942014-02-19 10:27:53 -050042 {
Zhenyao Modb9b40b2014-10-29 15:00:04 -070043 case SH_WEBGL_SPEC:
44 case SH_CSS_SHADERS_SPEC:
Jamie Madill88f6e942014-02-19 10:27:53 -050045 return 256;
Zhenyao Modb9b40b2014-10-29 15:00:04 -070046 default:
Jamie Madill88f6e942014-02-19 10:27:53 -050047 return 1024;
48 }
49}
50
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +000051namespace {
Zhenyao Modb9b40b2014-10-29 15:00:04 -070052
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080053class TScopedPoolAllocator
54{
55 public:
56 TScopedPoolAllocator(TPoolAllocator* allocator) : mAllocator(allocator)
57 {
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -040058 mAllocator->push();
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +000059 SetGlobalPoolAllocator(mAllocator);
60 }
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080061 ~TScopedPoolAllocator()
62 {
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +000063 SetGlobalPoolAllocator(NULL);
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -040064 mAllocator->pop();
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +000065 }
66
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080067 private:
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +000068 TPoolAllocator* mAllocator;
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -040069};
70
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080071class TScopedSymbolTableLevel
72{
73 public:
74 TScopedSymbolTableLevel(TSymbolTable* table) : mTable(table)
75 {
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -040076 ASSERT(mTable->atBuiltInLevel());
77 mTable->push();
78 }
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080079 ~TScopedSymbolTableLevel()
80 {
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -040081 while (!mTable->atBuiltInLevel())
82 mTable->pop();
83 }
84
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080085 private:
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -040086 TSymbolTable* mTable;
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +000087};
Zhenyao Modb9b40b2014-10-29 15:00:04 -070088
89int MapSpecToShaderVersion(ShShaderSpec spec)
90{
91 switch (spec)
92 {
93 case SH_GLES2_SPEC:
94 case SH_WEBGL_SPEC:
95 case SH_CSS_SHADERS_SPEC:
96 return 100;
97 case SH_GLES3_SPEC:
98 case SH_WEBGL2_SPEC:
99 return 300;
100 default:
101 UNREACHABLE();
102 return 0;
103 }
104}
105
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000106} // namespace
107
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800108TShHandleBase::TShHandleBase()
109{
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000110 allocator.push();
111 SetGlobalPoolAllocator(&allocator);
112}
113
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800114TShHandleBase::~TShHandleBase()
115{
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000116 SetGlobalPoolAllocator(NULL);
117 allocator.popAll();
118}
119
Jamie Madill183bde52014-07-02 15:31:19 -0400120TCompiler::TCompiler(sh::GLenum type, ShShaderSpec spec, ShShaderOutput output)
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000121 : shaderType(type),
zmo@google.comf420c422011-09-12 18:27:59 +0000122 shaderSpec(spec),
Jamie Madill68fe74a2014-05-27 12:56:01 -0400123 outputType(output),
Jamie Madilleb1a0102013-07-08 13:31:38 -0400124 maxUniformVectors(0),
125 maxExpressionComplexity(0),
126 maxCallStackDepth(0),
shannon.woods%transgaming.com@gtempaccount.comcbb6b6a2013-04-13 03:27:47 +0000127 fragmentPrecisionHigh(false),
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000128 clampingStrategy(SH_CLAMP_WITH_CLAMP_INTRINSIC),
zmo@google.com9996b8e2012-01-19 01:43:55 +0000129 builtInFunctionEmulator(type)
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000130{
131}
132
133TCompiler::~TCompiler()
134{
135}
136
137bool TCompiler::Init(const ShBuiltInResources& resources)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000138{
shannon.woods%transgaming.com@gtempaccount.com0bbed382013-04-13 03:38:07 +0000139 shaderVersion = 100;
Jamie Madill183bde52014-07-02 15:31:19 -0400140 maxUniformVectors = (shaderType == GL_VERTEX_SHADER) ?
gman@chromium.org8d804792012-10-17 21:33:48 +0000141 resources.MaxVertexUniformVectors :
142 resources.MaxFragmentUniformVectors;
Jamie Madilleb1a0102013-07-08 13:31:38 -0400143 maxExpressionComplexity = resources.MaxExpressionComplexity;
144 maxCallStackDepth = resources.MaxCallStackDepth;
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400145
146 SetGlobalPoolAllocator(&allocator);
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000147
alokp@chromium.org07620a52010-09-23 17:53:56 +0000148 // Generate built-in symbol table.
149 if (!InitBuiltInSymbolTable(resources))
150 return false;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000151 InitExtensionBehavior(resources, extensionBehavior);
shannon.woods%transgaming.com@gtempaccount.comcbb6b6a2013-04-13 03:27:47 +0000152 fragmentPrecisionHigh = resources.FragmentPrecisionHigh == 1;
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000153
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000154 arrayBoundsClamper.SetClampingStrategy(resources.ArrayIndexClampingStrategy);
155 clampingStrategy = resources.ArrayIndexClampingStrategy;
156
daniel@transgaming.comc23f4612012-11-28 19:42:57 +0000157 hashFunction = resources.HashFunction;
158
alokp@chromium.org07620a52010-09-23 17:53:56 +0000159 return true;
160}
161
162bool TCompiler::compile(const char* const shaderStrings[],
shannon.woods@transgaming.comd64b3da2013-02-28 23:19:26 +0000163 size_t numStrings,
alokp@chromium.org07620a52010-09-23 17:53:56 +0000164 int compileOptions)
165{
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400166 TScopedPoolAllocator scopedAlloc(&allocator);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000167 clearResults();
168
169 if (numStrings == 0)
170 return true;
171
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000172 // If compiling for WebGL, validate loop and indexing as well.
Jamie Madill5508f392014-02-20 13:31:36 -0500173 if (IsWebGLBasedSpec(shaderSpec))
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000174 compileOptions |= SH_VALIDATE_LOOP_INDEXING;
alokp@chromium.org1f299542010-11-12 15:50:23 +0000175
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000176 // First string is path of source file if flag is set. The actual source follows.
177 const char* sourcePath = NULL;
shannon.woods@transgaming.comd64b3da2013-02-28 23:19:26 +0000178 size_t firstSource = 0;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000179 if (compileOptions & SH_SOURCE_PATH)
180 {
181 sourcePath = shaderStrings[0];
182 ++firstSource;
183 }
184
alokp@chromium.org07620a52010-09-23 17:53:56 +0000185 TIntermediate intermediate(infoSink);
186 TParseContext parseContext(symbolTable, extensionBehavior, intermediate,
zmo@google.comdc4b4f82011-06-17 00:42:53 +0000187 shaderType, shaderSpec, compileOptions, true,
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000188 sourcePath, infoSink);
shannon.woods%transgaming.com@gtempaccount.comcbb6b6a2013-04-13 03:27:47 +0000189 parseContext.fragmentPrecisionHigh = fragmentPrecisionHigh;
Alok Priyadarshi8156b6b2013-09-23 14:56:58 -0400190 SetGlobalParseContext(&parseContext);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000191
192 // We preserve symbols at the built-in level from compile-to-compile.
193 // Start pushing the user-defined symbols at global level.
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400194 TScopedSymbolTableLevel scopedSymbolLevel(&symbolTable);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000195
196 // Parse shader.
197 bool success =
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000198 (PaParseStrings(numStrings - firstSource, &shaderStrings[firstSource], NULL, &parseContext) == 0) &&
alokp@chromium.org07620a52010-09-23 17:53:56 +0000199 (parseContext.treeRoot != NULL);
shannon.woods%transgaming.com@gtempaccount.com0bbed382013-04-13 03:38:07 +0000200
shannon.woods%transgaming.com@gtempaccount.com5524db02013-04-13 03:38:16 +0000201 shaderVersion = parseContext.getShaderVersion();
Zhenyao Modb9b40b2014-10-29 15:00:04 -0700202 if (success && MapSpecToShaderVersion(shaderSpec) < shaderVersion)
203 {
204 infoSink.info.prefix(EPrefixError);
205 infoSink.info << "unsupported shader version";
206 success = false;
207 }
shannon.woods%transgaming.com@gtempaccount.com0bbed382013-04-13 03:38:07 +0000208
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800209 if (success)
210 {
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700211 mPragma = parseContext.pragma();
212 if (mPragma.stdgl.invariantAll)
213 {
214 symbolTable.setGlobalInvariant();
215 }
216
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000217 TIntermNode* root = parseContext.treeRoot;
218 success = intermediate.postProcess(root);
219
Jamie Madill6654bc92014-03-26 14:01:57 -0400220 // Disallow expressions deemed too complex.
221 if (success && (compileOptions & SH_LIMIT_EXPRESSION_COMPLEXITY))
222 success = limitExpressionComplexity(root);
223
zmo@google.comb1762df2011-07-30 02:04:23 +0000224 if (success)
Jamie Madilleb1a0102013-07-08 13:31:38 -0400225 success = detectCallDepth(root, infoSink, (compileOptions & SH_LIMIT_CALL_STACK_DEPTH) != 0);
zmo@google.comb1762df2011-07-30 02:04:23 +0000226
Jamie Madill183bde52014-07-02 15:31:19 -0400227 if (success && shaderVersion == 300 && shaderType == GL_FRAGMENT_SHADER)
Jamie Madill05a80ce2013-06-20 11:55:49 -0400228 success = validateOutputs(root);
229
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000230 if (success && (compileOptions & SH_VALIDATE_LOOP_INDEXING))
231 success = validateLimitations(root);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000232
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000233 if (success && (compileOptions & SH_TIMING_RESTRICTIONS))
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000234 success = enforceTimingRestrictions(root, (compileOptions & SH_DEPENDENCY_GRAPH) != 0);
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000235
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +0000236 if (success && shaderSpec == SH_CSS_SHADERS_SPEC)
237 rewriteCSSShader(root);
238
zmo@google.com0c6bb7a2011-08-17 19:39:58 +0000239 // Unroll for-loop markup needs to happen after validateLimitations pass.
240 if (success && (compileOptions & SH_UNROLL_FOR_LOOP_WITH_INTEGER_INDEX))
Zhenyao Mo3cdfcce2014-03-07 13:00:08 -0800241 {
242 ForLoopUnrollMarker marker(ForLoopUnrollMarker::kIntegerIndex);
Zhenyao Mo550c6002014-02-26 15:40:48 -0800243 root->traverse(&marker);
244 }
245 if (success && (compileOptions & SH_UNROLL_FOR_LOOP_WITH_SAMPLER_ARRAY_INDEX))
Zhenyao Mo3cdfcce2014-03-07 13:00:08 -0800246 {
247 ForLoopUnrollMarker marker(ForLoopUnrollMarker::kSamplerArrayIndex);
Zhenyao Mo550c6002014-02-26 15:40:48 -0800248 root->traverse(&marker);
249 if (marker.samplerArrayIndexIsFloatLoopIndex())
250 {
251 infoSink.info.prefix(EPrefixError);
252 infoSink.info << "sampler array index is float loop index";
253 success = false;
254 }
255 }
zmo@google.com0c6bb7a2011-08-17 19:39:58 +0000256
zmo@google.com32e97312011-08-24 01:03:11 +0000257 // Built-in function emulation needs to happen after validateLimitations pass.
258 if (success && (compileOptions & SH_EMULATE_BUILT_IN_FUNCTIONS))
259 builtInFunctionEmulator.MarkBuiltInFunctionsForEmulation(root);
260
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000261 // Clamping uniform array bounds needs to happen after validateLimitations pass.
262 if (success && (compileOptions & SH_CLAMP_INDIRECT_ARRAY_BOUNDS))
263 arrayBoundsClamper.MarkIndirectArrayBoundsForClamping(root);
264
Jamie Madill183bde52014-07-02 15:31:19 -0400265 if (success && shaderType == GL_VERTEX_SHADER && (compileOptions & SH_INIT_GL_POSITION))
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800266 initializeGLPosition(root);
Zhenyao Moac44cd22013-09-23 14:57:09 -0400267
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800268 if (success && (compileOptions & SH_UNFOLD_SHORT_CIRCUIT))
269 {
Zhenyao Mo7cab38b2013-10-15 12:59:30 -0700270 UnfoldShortCircuitAST unfoldShortCircuit;
271 root->traverse(&unfoldShortCircuit);
272 unfoldShortCircuit.updateTree();
273 }
274
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800275 if (success && (compileOptions & SH_VARIABLES))
276 {
Zhenyao Mo74da9f22013-09-23 14:57:01 -0400277 collectVariables(root);
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800278 if (compileOptions & SH_ENFORCE_PACKING_RESTRICTIONS)
279 {
gman@chromium.org8d804792012-10-17 21:33:48 +0000280 success = enforcePackingRestrictions();
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800281 if (!success)
282 {
Jamie Madill075edd82013-07-08 13:30:19 -0400283 infoSink.info.prefix(EPrefixError);
284 infoSink.info << "too many uniforms";
gman@chromium.org8d804792012-10-17 21:33:48 +0000285 }
286 }
Jamie Madill183bde52014-07-02 15:31:19 -0400287 if (success && shaderType == GL_VERTEX_SHADER &&
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800288 (compileOptions & SH_INIT_VARYINGS_WITHOUT_STATIC_USE))
289 initializeVaryingsWithoutStaticUse(root);
gman@chromium.org8d804792012-10-17 21:33:48 +0000290 }
zmo@google.comfd747b82011-04-23 01:30:07 +0000291
Zhenyao Mocd68fe72014-07-11 10:45:44 -0700292 if (success && (compileOptions & SH_SCALARIZE_VEC_AND_MAT_CONSTRUCTOR_ARGS))
293 {
Zhenyao Modaf56572014-08-06 16:18:30 -0700294 ScalarizeVecAndMatConstructorArgs scalarizer(
295 shaderType, fragmentPrecisionHigh);
Zhenyao Mocd68fe72014-07-11 10:45:44 -0700296 root->traverse(&scalarizer);
297 }
298
Zhenyao Moe740add2014-07-18 17:01:01 -0700299 if (success && (compileOptions & SH_REGENERATE_STRUCT_NAMES))
300 {
301 RegenerateStructNames gen(symbolTable, shaderVersion);
302 root->traverse(&gen);
303 }
304
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000305 if (success && (compileOptions & SH_INTERMEDIATE_TREE))
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000306 intermediate.outputTree(root);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000307
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000308 if (success && (compileOptions & SH_OBJECT_CODE))
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000309 translate(root);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000310 }
311
312 // Cleanup memory.
313 intermediate.remove(parseContext.treeRoot);
Zhenyao Mo7faf1a12014-04-25 18:03:56 -0700314 SetGlobalParseContext(NULL);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000315 return success;
316}
317
Nicolas Capens49a88872013-06-20 09:54:03 -0400318bool TCompiler::InitBuiltInSymbolTable(const ShBuiltInResources &resources)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000319{
shannon.woods%transgaming.com@gtempaccount.com18b4c4b2013-04-13 03:31:40 +0000320 compileResources = resources;
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400321 setResourceString();
shannonwoods@chromium.org2ac0be92013-05-30 00:02:27 +0000322
Nicolas Capens49a88872013-06-20 09:54:03 -0400323 assert(symbolTable.isEmpty());
324 symbolTable.push(); // COMMON_BUILTINS
325 symbolTable.push(); // ESSL1_BUILTINS
326 symbolTable.push(); // ESSL3_BUILTINS
shannonwoods@chromium.org2ac0be92013-05-30 00:02:27 +0000327
Nicolas Capens49a88872013-06-20 09:54:03 -0400328 TPublicType integer;
329 integer.type = EbtInt;
330 integer.primarySize = 1;
331 integer.secondarySize = 1;
332 integer.array = false;
333
334 TPublicType floatingPoint;
335 floatingPoint.type = EbtFloat;
336 floatingPoint.primarySize = 1;
337 floatingPoint.secondarySize = 1;
338 floatingPoint.array = false;
339
Zhenyao Moa5a1dfc2013-09-23 14:57:03 -0400340 TPublicType sampler;
341 sampler.primarySize = 1;
342 sampler.secondarySize = 1;
343 sampler.array = false;
344
Nicolas Capens49a88872013-06-20 09:54:03 -0400345 switch(shaderType)
346 {
Jamie Madill183bde52014-07-02 15:31:19 -0400347 case GL_FRAGMENT_SHADER:
Nicolas Capens49a88872013-06-20 09:54:03 -0400348 symbolTable.setDefaultPrecision(integer, EbpMedium);
349 break;
Jamie Madill183bde52014-07-02 15:31:19 -0400350 case GL_VERTEX_SHADER:
Nicolas Capens49a88872013-06-20 09:54:03 -0400351 symbolTable.setDefaultPrecision(integer, EbpHigh);
352 symbolTable.setDefaultPrecision(floatingPoint, EbpHigh);
353 break;
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800354 default:
355 assert(false && "Language not supported");
Nicolas Capens49a88872013-06-20 09:54:03 -0400356 }
Zhenyao Moa5a1dfc2013-09-23 14:57:03 -0400357 // We set defaults for all the sampler types, even those that are
358 // only available if an extension exists.
359 for (int samplerType = EbtGuardSamplerBegin + 1;
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800360 samplerType < EbtGuardSamplerEnd; ++samplerType)
361 {
Zhenyao Moa5a1dfc2013-09-23 14:57:03 -0400362 sampler.type = static_cast<TBasicType>(samplerType);
363 symbolTable.setDefaultPrecision(sampler, EbpLow);
364 }
Nicolas Capens49a88872013-06-20 09:54:03 -0400365
Jamie Madill1b452142013-07-12 14:51:11 -0400366 InsertBuiltInFunctions(shaderType, shaderSpec, resources, symbolTable);
Nicolas Capens49a88872013-06-20 09:54:03 -0400367
368 IdentifyBuiltIns(shaderType, shaderSpec, resources, symbolTable);
369
370 return true;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000371}
372
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400373void TCompiler::setResourceString()
374{
375 std::ostringstream strstream;
376 strstream << ":MaxVertexAttribs:" << compileResources.MaxVertexAttribs
377 << ":MaxVertexUniformVectors:" << compileResources.MaxVertexUniformVectors
378 << ":MaxVaryingVectors:" << compileResources.MaxVaryingVectors
379 << ":MaxVertexTextureImageUnits:" << compileResources.MaxVertexTextureImageUnits
380 << ":MaxCombinedTextureImageUnits:" << compileResources.MaxCombinedTextureImageUnits
381 << ":MaxTextureImageUnits:" << compileResources.MaxTextureImageUnits
382 << ":MaxFragmentUniformVectors:" << compileResources.MaxFragmentUniformVectors
383 << ":MaxDrawBuffers:" << compileResources.MaxDrawBuffers
384 << ":OES_standard_derivatives:" << compileResources.OES_standard_derivatives
385 << ":OES_EGL_image_external:" << compileResources.OES_EGL_image_external
386 << ":ARB_texture_rectangle:" << compileResources.ARB_texture_rectangle
387 << ":EXT_draw_buffers:" << compileResources.EXT_draw_buffers
388 << ":FragmentPrecisionHigh:" << compileResources.FragmentPrecisionHigh
389 << ":MaxExpressionComplexity:" << compileResources.MaxExpressionComplexity
390 << ":MaxCallStackDepth:" << compileResources.MaxCallStackDepth
391 << ":EXT_frag_depth:" << compileResources.EXT_frag_depth
392 << ":EXT_shader_texture_lod:" << compileResources.EXT_shader_texture_lod
393 << ":MaxVertexOutputVectors:" << compileResources.MaxVertexOutputVectors
394 << ":MaxFragmentInputVectors:" << compileResources.MaxFragmentInputVectors
395 << ":MinProgramTexelOffset:" << compileResources.MinProgramTexelOffset
Olli Etuahoe61209a2014-09-26 12:01:17 +0300396 << ":MaxProgramTexelOffset:" << compileResources.MaxProgramTexelOffset
397 << ":NV_draw_buffers:" << compileResources.NV_draw_buffers;
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400398
399 builtInResourcesString = strstream.str();
400}
401
alokp@chromium.org07620a52010-09-23 17:53:56 +0000402void TCompiler::clearResults()
403{
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000404 arrayBoundsClamper.Cleanup();
alokp@chromium.org07620a52010-09-23 17:53:56 +0000405 infoSink.info.erase();
406 infoSink.obj.erase();
407 infoSink.debug.erase();
408
Jamie Madilled27c722014-07-02 15:31:23 -0400409 attributes.clear();
410 outputVariables.clear();
alokp@chromium.org07620a52010-09-23 17:53:56 +0000411 uniforms.clear();
Jamie Madill23a8a432014-07-09 13:27:42 -0400412 expandedUniforms.clear();
Zhenyao Mod2d340b2013-09-23 14:57:05 -0400413 varyings.clear();
Jamie Madilled27c722014-07-02 15:31:23 -0400414 interfaceBlocks.clear();
zmo@google.coma3b4ab42011-09-16 00:53:26 +0000415
416 builtInFunctionEmulator.Cleanup();
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +0000417
418 nameMap.clear();
alokp@chromium.org07620a52010-09-23 17:53:56 +0000419}
420
Jamie Madilleb1a0102013-07-08 13:31:38 -0400421bool TCompiler::detectCallDepth(TIntermNode* root, TInfoSink& infoSink, bool limitCallStackDepth)
zmo@google.comb1762df2011-07-30 02:04:23 +0000422{
Jamie Madilleb1a0102013-07-08 13:31:38 -0400423 DetectCallDepth detect(infoSink, limitCallStackDepth, maxCallStackDepth);
zmo@google.comb1762df2011-07-30 02:04:23 +0000424 root->traverse(&detect);
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800425 switch (detect.detectCallDepth())
426 {
427 case DetectCallDepth::kErrorNone:
428 return true;
429 case DetectCallDepth::kErrorMissingMain:
430 infoSink.info.prefix(EPrefixError);
431 infoSink.info << "Missing main()";
432 return false;
433 case DetectCallDepth::kErrorRecursion:
434 infoSink.info.prefix(EPrefixError);
435 infoSink.info << "Function recursion detected";
436 return false;
437 case DetectCallDepth::kErrorMaxDepthExceeded:
438 infoSink.info.prefix(EPrefixError);
439 infoSink.info << "Function call stack too deep";
440 return false;
441 default:
442 UNREACHABLE();
443 return false;
zmo@google.comb1762df2011-07-30 02:04:23 +0000444 }
445}
446
Jamie Madill05a80ce2013-06-20 11:55:49 -0400447bool TCompiler::validateOutputs(TIntermNode* root)
448{
449 ValidateOutputs validateOutputs(infoSink.info, compileResources.MaxDrawBuffers);
450 root->traverse(&validateOutputs);
451 return (validateOutputs.numErrors() == 0);
452}
453
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +0000454void TCompiler::rewriteCSSShader(TIntermNode* root)
455{
456 RenameFunction renamer("main(", "css_main(");
457 root->traverse(&renamer);
458}
459
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800460bool TCompiler::validateLimitations(TIntermNode* root)
461{
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000462 ValidateLimitations validate(shaderType, infoSink.info);
463 root->traverse(&validate);
464 return validate.numErrors() == 0;
465}
466
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000467bool TCompiler::enforceTimingRestrictions(TIntermNode* root, bool outputGraph)
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000468{
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800469 if (shaderSpec != SH_WEBGL_SPEC)
470 {
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000471 infoSink.info << "Timing restrictions must be enforced under the WebGL spec.";
472 return false;
473 }
474
Jamie Madill183bde52014-07-02 15:31:19 -0400475 if (shaderType == GL_FRAGMENT_SHADER)
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800476 {
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000477 TDependencyGraph graph(root);
478
479 // Output any errors first.
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000480 bool success = enforceFragmentShaderTimingRestrictions(graph);
Jamie Madill5508f392014-02-20 13:31:36 -0500481
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000482 // Then, output the dependency graph.
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800483 if (outputGraph)
484 {
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000485 TDependencyGraphOutput output(infoSink.info);
486 output.outputAllSpanningTrees(graph);
487 }
Jamie Madill5508f392014-02-20 13:31:36 -0500488
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000489 return success;
490 }
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800491 else
492 {
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000493 return enforceVertexShaderTimingRestrictions(root);
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000494 }
495}
496
Jamie Madilleb1a0102013-07-08 13:31:38 -0400497bool TCompiler::limitExpressionComplexity(TIntermNode* root)
498{
Jamie Madill6654bc92014-03-26 14:01:57 -0400499 TMaxDepthTraverser traverser(maxExpressionComplexity+1);
Jamie Madilleb1a0102013-07-08 13:31:38 -0400500 root->traverse(&traverser);
Jamie Madill6654bc92014-03-26 14:01:57 -0400501
502 if (traverser.getMaxDepth() > maxExpressionComplexity)
503 {
504 infoSink.info << "Expression too complex.";
505 return false;
506 }
507
Jamie Madilleb1a0102013-07-08 13:31:38 -0400508 TDependencyGraph graph(root);
509
510 for (TFunctionCallVector::const_iterator iter = graph.beginUserDefinedFunctionCalls();
511 iter != graph.endUserDefinedFunctionCalls();
512 ++iter)
513 {
514 TGraphFunctionCall* samplerSymbol = *iter;
515 TDependencyGraphTraverser graphTraverser;
516 samplerSymbol->traverse(&graphTraverser);
517 }
518
Jamie Madilleb1a0102013-07-08 13:31:38 -0400519 return true;
520}
521
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000522bool TCompiler::enforceFragmentShaderTimingRestrictions(const TDependencyGraph& graph)
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000523{
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000524 RestrictFragmentShaderTiming restrictor(infoSink.info);
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000525 restrictor.enforceRestrictions(graph);
526 return restrictor.numErrors() == 0;
527}
528
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000529bool TCompiler::enforceVertexShaderTimingRestrictions(TIntermNode* root)
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000530{
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000531 RestrictVertexShaderTiming restrictor(infoSink.info);
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000532 restrictor.enforceRestrictions(root);
533 return restrictor.numErrors() == 0;
534}
535
Zhenyao Mo74da9f22013-09-23 14:57:01 -0400536void TCompiler::collectVariables(TIntermNode* root)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000537{
Jamie Madilla2fbb842014-09-03 09:40:47 -0400538 sh::CollectVariables collect(&attributes,
539 &outputVariables,
540 &uniforms,
541 &varyings,
542 &interfaceBlocks,
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700543 hashFunction,
544 symbolTable);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000545 root->traverse(&collect);
Jamie Madill23a8a432014-07-09 13:27:42 -0400546
Zhenyao Mo409078f2014-10-28 13:23:18 -0700547 // This is for enforcePackingRestriction().
548 sh::ExpandUniforms(uniforms, &expandedUniforms);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000549}
zmo@google.comfd747b82011-04-23 01:30:07 +0000550
gman@chromium.org8d804792012-10-17 21:33:48 +0000551bool TCompiler::enforcePackingRestrictions()
552{
553 VariablePacker packer;
Jamie Madill23a8a432014-07-09 13:27:42 -0400554 return packer.CheckVariablesWithinPackingLimits(maxUniformVectors, expandedUniforms);
gman@chromium.org8d804792012-10-17 21:33:48 +0000555}
556
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800557void TCompiler::initializeGLPosition(TIntermNode* root)
558{
559 InitializeVariables::InitVariableInfoList variables;
560 InitializeVariables::InitVariableInfo var(
561 "gl_Position", TType(EbtFloat, EbpUndefined, EvqPosition, 4));
562 variables.push_back(var);
563 InitializeVariables initializer(variables);
564 root->traverse(&initializer);
565}
566
567void TCompiler::initializeVaryingsWithoutStaticUse(TIntermNode* root)
568{
569 InitializeVariables::InitVariableInfoList variables;
570 for (size_t ii = 0; ii < varyings.size(); ++ii)
571 {
Jamie Madilla718c1e2014-07-02 15:31:22 -0400572 const sh::Varying& varying = varyings[ii];
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800573 if (varying.staticUse)
574 continue;
Jamie Madillaa72d782014-07-02 15:31:19 -0400575 unsigned char primarySize = static_cast<unsigned char>(gl::VariableColumnCount(varying.type));
576 unsigned char secondarySize = static_cast<unsigned char>(gl::VariableRowCount(varying.type));
Jamie Madilla718c1e2014-07-02 15:31:22 -0400577 TType type(EbtFloat, EbpUndefined, EvqVaryingOut, primarySize, secondarySize, varying.isArray());
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800578 TString name = varying.name.c_str();
Jamie Madilla718c1e2014-07-02 15:31:22 -0400579 if (varying.isArray())
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800580 {
Jamie Madilla718c1e2014-07-02 15:31:22 -0400581 type.setArraySize(varying.arraySize);
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800582 name = name.substr(0, name.find_first_of('['));
583 }
584
585 InitializeVariables::InitVariableInfo var(name, type);
586 variables.push_back(var);
587 }
588 InitializeVariables initializer(variables);
589 root->traverse(&initializer);
590}
591
zmo@google.com5601ea02011-06-10 18:23:25 +0000592const TExtensionBehavior& TCompiler::getExtensionBehavior() const
593{
594 return extensionBehavior;
595}
zmo@google.com32e97312011-08-24 01:03:11 +0000596
shannon.woods%transgaming.com@gtempaccount.com18b4c4b2013-04-13 03:31:40 +0000597const ShBuiltInResources& TCompiler::getResources() const
598{
599 return compileResources;
600}
601
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000602const ArrayBoundsClamper& TCompiler::getArrayBoundsClamper() const
603{
604 return arrayBoundsClamper;
605}
606
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000607ShArrayIndexClampingStrategy TCompiler::getArrayIndexClampingStrategy() const
608{
609 return clampingStrategy;
610}
611
612const BuiltInFunctionEmulator& TCompiler::getBuiltInFunctionEmulator() const
613{
614 return builtInFunctionEmulator;
615}
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700616
617void TCompiler::writePragma()
618{
619 TInfoSinkBase &sink = infoSink.obj;
620 if (mPragma.stdgl.invariantAll)
621 sink << "#pragma STDGL invariant(all)\n";
622}