blob: ab1c9dd0281968935c3f8d38f9b778f9efe591bb [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
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200185 bool debugShaderPrecision = getResources().WEBGL_debug_shader_precision == 1;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000186 TIntermediate intermediate(infoSink);
187 TParseContext parseContext(symbolTable, extensionBehavior, intermediate,
zmo@google.comdc4b4f82011-06-17 00:42:53 +0000188 shaderType, shaderSpec, compileOptions, true,
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200189 sourcePath, infoSink, debugShaderPrecision);
190
shannon.woods%transgaming.com@gtempaccount.comcbb6b6a2013-04-13 03:27:47 +0000191 parseContext.fragmentPrecisionHigh = fragmentPrecisionHigh;
Alok Priyadarshi8156b6b2013-09-23 14:56:58 -0400192 SetGlobalParseContext(&parseContext);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000193
194 // We preserve symbols at the built-in level from compile-to-compile.
195 // Start pushing the user-defined symbols at global level.
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400196 TScopedSymbolTableLevel scopedSymbolLevel(&symbolTable);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000197
198 // Parse shader.
199 bool success =
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000200 (PaParseStrings(numStrings - firstSource, &shaderStrings[firstSource], NULL, &parseContext) == 0) &&
alokp@chromium.org07620a52010-09-23 17:53:56 +0000201 (parseContext.treeRoot != NULL);
shannon.woods%transgaming.com@gtempaccount.com0bbed382013-04-13 03:38:07 +0000202
shannon.woods%transgaming.com@gtempaccount.com5524db02013-04-13 03:38:16 +0000203 shaderVersion = parseContext.getShaderVersion();
Zhenyao Modb9b40b2014-10-29 15:00:04 -0700204 if (success && MapSpecToShaderVersion(shaderSpec) < shaderVersion)
205 {
206 infoSink.info.prefix(EPrefixError);
207 infoSink.info << "unsupported shader version";
208 success = false;
209 }
shannon.woods%transgaming.com@gtempaccount.com0bbed382013-04-13 03:38:07 +0000210
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800211 if (success)
212 {
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700213 mPragma = parseContext.pragma();
214 if (mPragma.stdgl.invariantAll)
215 {
216 symbolTable.setGlobalInvariant();
217 }
218
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000219 TIntermNode* root = parseContext.treeRoot;
220 success = intermediate.postProcess(root);
221
Jamie Madill6654bc92014-03-26 14:01:57 -0400222 // Disallow expressions deemed too complex.
223 if (success && (compileOptions & SH_LIMIT_EXPRESSION_COMPLEXITY))
224 success = limitExpressionComplexity(root);
225
zmo@google.comb1762df2011-07-30 02:04:23 +0000226 if (success)
Jamie Madilleb1a0102013-07-08 13:31:38 -0400227 success = detectCallDepth(root, infoSink, (compileOptions & SH_LIMIT_CALL_STACK_DEPTH) != 0);
zmo@google.comb1762df2011-07-30 02:04:23 +0000228
Jamie Madill183bde52014-07-02 15:31:19 -0400229 if (success && shaderVersion == 300 && shaderType == GL_FRAGMENT_SHADER)
Jamie Madill05a80ce2013-06-20 11:55:49 -0400230 success = validateOutputs(root);
231
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000232 if (success && (compileOptions & SH_VALIDATE_LOOP_INDEXING))
233 success = validateLimitations(root);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000234
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000235 if (success && (compileOptions & SH_TIMING_RESTRICTIONS))
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000236 success = enforceTimingRestrictions(root, (compileOptions & SH_DEPENDENCY_GRAPH) != 0);
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000237
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +0000238 if (success && shaderSpec == SH_CSS_SHADERS_SPEC)
239 rewriteCSSShader(root);
240
zmo@google.com0c6bb7a2011-08-17 19:39:58 +0000241 // Unroll for-loop markup needs to happen after validateLimitations pass.
242 if (success && (compileOptions & SH_UNROLL_FOR_LOOP_WITH_INTEGER_INDEX))
Zhenyao Mo3cdfcce2014-03-07 13:00:08 -0800243 {
244 ForLoopUnrollMarker marker(ForLoopUnrollMarker::kIntegerIndex);
Zhenyao Mo550c6002014-02-26 15:40:48 -0800245 root->traverse(&marker);
246 }
247 if (success && (compileOptions & SH_UNROLL_FOR_LOOP_WITH_SAMPLER_ARRAY_INDEX))
Zhenyao Mo3cdfcce2014-03-07 13:00:08 -0800248 {
249 ForLoopUnrollMarker marker(ForLoopUnrollMarker::kSamplerArrayIndex);
Zhenyao Mo550c6002014-02-26 15:40:48 -0800250 root->traverse(&marker);
251 if (marker.samplerArrayIndexIsFloatLoopIndex())
252 {
253 infoSink.info.prefix(EPrefixError);
254 infoSink.info << "sampler array index is float loop index";
255 success = false;
256 }
257 }
zmo@google.com0c6bb7a2011-08-17 19:39:58 +0000258
zmo@google.com32e97312011-08-24 01:03:11 +0000259 // Built-in function emulation needs to happen after validateLimitations pass.
260 if (success && (compileOptions & SH_EMULATE_BUILT_IN_FUNCTIONS))
261 builtInFunctionEmulator.MarkBuiltInFunctionsForEmulation(root);
262
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000263 // Clamping uniform array bounds needs to happen after validateLimitations pass.
264 if (success && (compileOptions & SH_CLAMP_INDIRECT_ARRAY_BOUNDS))
265 arrayBoundsClamper.MarkIndirectArrayBoundsForClamping(root);
266
Jamie Madill183bde52014-07-02 15:31:19 -0400267 if (success && shaderType == GL_VERTEX_SHADER && (compileOptions & SH_INIT_GL_POSITION))
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800268 initializeGLPosition(root);
Zhenyao Moac44cd22013-09-23 14:57:09 -0400269
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800270 if (success && (compileOptions & SH_UNFOLD_SHORT_CIRCUIT))
271 {
Zhenyao Mo7cab38b2013-10-15 12:59:30 -0700272 UnfoldShortCircuitAST unfoldShortCircuit;
273 root->traverse(&unfoldShortCircuit);
274 unfoldShortCircuit.updateTree();
275 }
276
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800277 if (success && (compileOptions & SH_VARIABLES))
278 {
Zhenyao Mo74da9f22013-09-23 14:57:01 -0400279 collectVariables(root);
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800280 if (compileOptions & SH_ENFORCE_PACKING_RESTRICTIONS)
281 {
gman@chromium.org8d804792012-10-17 21:33:48 +0000282 success = enforcePackingRestrictions();
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800283 if (!success)
284 {
Jamie Madill075edd82013-07-08 13:30:19 -0400285 infoSink.info.prefix(EPrefixError);
286 infoSink.info << "too many uniforms";
gman@chromium.org8d804792012-10-17 21:33:48 +0000287 }
288 }
Jamie Madill183bde52014-07-02 15:31:19 -0400289 if (success && shaderType == GL_VERTEX_SHADER &&
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800290 (compileOptions & SH_INIT_VARYINGS_WITHOUT_STATIC_USE))
291 initializeVaryingsWithoutStaticUse(root);
gman@chromium.org8d804792012-10-17 21:33:48 +0000292 }
zmo@google.comfd747b82011-04-23 01:30:07 +0000293
Zhenyao Mocd68fe72014-07-11 10:45:44 -0700294 if (success && (compileOptions & SH_SCALARIZE_VEC_AND_MAT_CONSTRUCTOR_ARGS))
295 {
Zhenyao Modaf56572014-08-06 16:18:30 -0700296 ScalarizeVecAndMatConstructorArgs scalarizer(
297 shaderType, fragmentPrecisionHigh);
Zhenyao Mocd68fe72014-07-11 10:45:44 -0700298 root->traverse(&scalarizer);
299 }
300
Zhenyao Moe740add2014-07-18 17:01:01 -0700301 if (success && (compileOptions & SH_REGENERATE_STRUCT_NAMES))
302 {
303 RegenerateStructNames gen(symbolTable, shaderVersion);
304 root->traverse(&gen);
305 }
306
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000307 if (success && (compileOptions & SH_INTERMEDIATE_TREE))
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000308 intermediate.outputTree(root);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000309
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000310 if (success && (compileOptions & SH_OBJECT_CODE))
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000311 translate(root);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000312 }
313
314 // Cleanup memory.
315 intermediate.remove(parseContext.treeRoot);
Zhenyao Mo7faf1a12014-04-25 18:03:56 -0700316 SetGlobalParseContext(NULL);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000317 return success;
318}
319
Nicolas Capens49a88872013-06-20 09:54:03 -0400320bool TCompiler::InitBuiltInSymbolTable(const ShBuiltInResources &resources)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000321{
shannon.woods%transgaming.com@gtempaccount.com18b4c4b2013-04-13 03:31:40 +0000322 compileResources = resources;
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400323 setResourceString();
shannonwoods@chromium.org2ac0be92013-05-30 00:02:27 +0000324
Nicolas Capens49a88872013-06-20 09:54:03 -0400325 assert(symbolTable.isEmpty());
326 symbolTable.push(); // COMMON_BUILTINS
327 symbolTable.push(); // ESSL1_BUILTINS
328 symbolTable.push(); // ESSL3_BUILTINS
shannonwoods@chromium.org2ac0be92013-05-30 00:02:27 +0000329
Nicolas Capens49a88872013-06-20 09:54:03 -0400330 TPublicType integer;
331 integer.type = EbtInt;
332 integer.primarySize = 1;
333 integer.secondarySize = 1;
334 integer.array = false;
335
336 TPublicType floatingPoint;
337 floatingPoint.type = EbtFloat;
338 floatingPoint.primarySize = 1;
339 floatingPoint.secondarySize = 1;
340 floatingPoint.array = false;
341
Zhenyao Moa5a1dfc2013-09-23 14:57:03 -0400342 TPublicType sampler;
343 sampler.primarySize = 1;
344 sampler.secondarySize = 1;
345 sampler.array = false;
346
Nicolas Capens49a88872013-06-20 09:54:03 -0400347 switch(shaderType)
348 {
Jamie Madill183bde52014-07-02 15:31:19 -0400349 case GL_FRAGMENT_SHADER:
Nicolas Capens49a88872013-06-20 09:54:03 -0400350 symbolTable.setDefaultPrecision(integer, EbpMedium);
351 break;
Jamie Madill183bde52014-07-02 15:31:19 -0400352 case GL_VERTEX_SHADER:
Nicolas Capens49a88872013-06-20 09:54:03 -0400353 symbolTable.setDefaultPrecision(integer, EbpHigh);
354 symbolTable.setDefaultPrecision(floatingPoint, EbpHigh);
355 break;
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800356 default:
357 assert(false && "Language not supported");
Nicolas Capens49a88872013-06-20 09:54:03 -0400358 }
Zhenyao Moa5a1dfc2013-09-23 14:57:03 -0400359 // We set defaults for all the sampler types, even those that are
360 // only available if an extension exists.
361 for (int samplerType = EbtGuardSamplerBegin + 1;
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800362 samplerType < EbtGuardSamplerEnd; ++samplerType)
363 {
Zhenyao Moa5a1dfc2013-09-23 14:57:03 -0400364 sampler.type = static_cast<TBasicType>(samplerType);
365 symbolTable.setDefaultPrecision(sampler, EbpLow);
366 }
Nicolas Capens49a88872013-06-20 09:54:03 -0400367
Jamie Madill1b452142013-07-12 14:51:11 -0400368 InsertBuiltInFunctions(shaderType, shaderSpec, resources, symbolTable);
Nicolas Capens49a88872013-06-20 09:54:03 -0400369
370 IdentifyBuiltIns(shaderType, shaderSpec, resources, symbolTable);
371
372 return true;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000373}
374
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400375void TCompiler::setResourceString()
376{
377 std::ostringstream strstream;
378 strstream << ":MaxVertexAttribs:" << compileResources.MaxVertexAttribs
379 << ":MaxVertexUniformVectors:" << compileResources.MaxVertexUniformVectors
380 << ":MaxVaryingVectors:" << compileResources.MaxVaryingVectors
381 << ":MaxVertexTextureImageUnits:" << compileResources.MaxVertexTextureImageUnits
382 << ":MaxCombinedTextureImageUnits:" << compileResources.MaxCombinedTextureImageUnits
383 << ":MaxTextureImageUnits:" << compileResources.MaxTextureImageUnits
384 << ":MaxFragmentUniformVectors:" << compileResources.MaxFragmentUniformVectors
385 << ":MaxDrawBuffers:" << compileResources.MaxDrawBuffers
386 << ":OES_standard_derivatives:" << compileResources.OES_standard_derivatives
387 << ":OES_EGL_image_external:" << compileResources.OES_EGL_image_external
388 << ":ARB_texture_rectangle:" << compileResources.ARB_texture_rectangle
389 << ":EXT_draw_buffers:" << compileResources.EXT_draw_buffers
390 << ":FragmentPrecisionHigh:" << compileResources.FragmentPrecisionHigh
391 << ":MaxExpressionComplexity:" << compileResources.MaxExpressionComplexity
392 << ":MaxCallStackDepth:" << compileResources.MaxCallStackDepth
393 << ":EXT_frag_depth:" << compileResources.EXT_frag_depth
394 << ":EXT_shader_texture_lod:" << compileResources.EXT_shader_texture_lod
395 << ":MaxVertexOutputVectors:" << compileResources.MaxVertexOutputVectors
396 << ":MaxFragmentInputVectors:" << compileResources.MaxFragmentInputVectors
397 << ":MinProgramTexelOffset:" << compileResources.MinProgramTexelOffset
Olli Etuahoe61209a2014-09-26 12:01:17 +0300398 << ":MaxProgramTexelOffset:" << compileResources.MaxProgramTexelOffset
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200399 << ":NV_draw_buffers:" << compileResources.NV_draw_buffers
400 << ":WEBGL_debug_shader_precision:" << compileResources.WEBGL_debug_shader_precision;
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400401
402 builtInResourcesString = strstream.str();
403}
404
alokp@chromium.org07620a52010-09-23 17:53:56 +0000405void TCompiler::clearResults()
406{
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000407 arrayBoundsClamper.Cleanup();
alokp@chromium.org07620a52010-09-23 17:53:56 +0000408 infoSink.info.erase();
409 infoSink.obj.erase();
410 infoSink.debug.erase();
411
Jamie Madilled27c722014-07-02 15:31:23 -0400412 attributes.clear();
413 outputVariables.clear();
alokp@chromium.org07620a52010-09-23 17:53:56 +0000414 uniforms.clear();
Jamie Madill23a8a432014-07-09 13:27:42 -0400415 expandedUniforms.clear();
Zhenyao Mod2d340b2013-09-23 14:57:05 -0400416 varyings.clear();
Jamie Madilled27c722014-07-02 15:31:23 -0400417 interfaceBlocks.clear();
zmo@google.coma3b4ab42011-09-16 00:53:26 +0000418
419 builtInFunctionEmulator.Cleanup();
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +0000420
421 nameMap.clear();
alokp@chromium.org07620a52010-09-23 17:53:56 +0000422}
423
Jamie Madilleb1a0102013-07-08 13:31:38 -0400424bool TCompiler::detectCallDepth(TIntermNode* root, TInfoSink& infoSink, bool limitCallStackDepth)
zmo@google.comb1762df2011-07-30 02:04:23 +0000425{
Jamie Madilleb1a0102013-07-08 13:31:38 -0400426 DetectCallDepth detect(infoSink, limitCallStackDepth, maxCallStackDepth);
zmo@google.comb1762df2011-07-30 02:04:23 +0000427 root->traverse(&detect);
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800428 switch (detect.detectCallDepth())
429 {
430 case DetectCallDepth::kErrorNone:
431 return true;
432 case DetectCallDepth::kErrorMissingMain:
433 infoSink.info.prefix(EPrefixError);
434 infoSink.info << "Missing main()";
435 return false;
436 case DetectCallDepth::kErrorRecursion:
437 infoSink.info.prefix(EPrefixError);
438 infoSink.info << "Function recursion detected";
439 return false;
440 case DetectCallDepth::kErrorMaxDepthExceeded:
441 infoSink.info.prefix(EPrefixError);
442 infoSink.info << "Function call stack too deep";
443 return false;
444 default:
445 UNREACHABLE();
446 return false;
zmo@google.comb1762df2011-07-30 02:04:23 +0000447 }
448}
449
Jamie Madill05a80ce2013-06-20 11:55:49 -0400450bool TCompiler::validateOutputs(TIntermNode* root)
451{
452 ValidateOutputs validateOutputs(infoSink.info, compileResources.MaxDrawBuffers);
453 root->traverse(&validateOutputs);
454 return (validateOutputs.numErrors() == 0);
455}
456
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +0000457void TCompiler::rewriteCSSShader(TIntermNode* root)
458{
459 RenameFunction renamer("main(", "css_main(");
460 root->traverse(&renamer);
461}
462
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800463bool TCompiler::validateLimitations(TIntermNode* root)
464{
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000465 ValidateLimitations validate(shaderType, infoSink.info);
466 root->traverse(&validate);
467 return validate.numErrors() == 0;
468}
469
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000470bool TCompiler::enforceTimingRestrictions(TIntermNode* root, bool outputGraph)
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000471{
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800472 if (shaderSpec != SH_WEBGL_SPEC)
473 {
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000474 infoSink.info << "Timing restrictions must be enforced under the WebGL spec.";
475 return false;
476 }
477
Jamie Madill183bde52014-07-02 15:31:19 -0400478 if (shaderType == GL_FRAGMENT_SHADER)
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800479 {
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000480 TDependencyGraph graph(root);
481
482 // Output any errors first.
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000483 bool success = enforceFragmentShaderTimingRestrictions(graph);
Jamie Madill5508f392014-02-20 13:31:36 -0500484
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000485 // Then, output the dependency graph.
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800486 if (outputGraph)
487 {
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000488 TDependencyGraphOutput output(infoSink.info);
489 output.outputAllSpanningTrees(graph);
490 }
Jamie Madill5508f392014-02-20 13:31:36 -0500491
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000492 return success;
493 }
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800494 else
495 {
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000496 return enforceVertexShaderTimingRestrictions(root);
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000497 }
498}
499
Jamie Madilleb1a0102013-07-08 13:31:38 -0400500bool TCompiler::limitExpressionComplexity(TIntermNode* root)
501{
Jamie Madill6654bc92014-03-26 14:01:57 -0400502 TMaxDepthTraverser traverser(maxExpressionComplexity+1);
Jamie Madilleb1a0102013-07-08 13:31:38 -0400503 root->traverse(&traverser);
Jamie Madill6654bc92014-03-26 14:01:57 -0400504
505 if (traverser.getMaxDepth() > maxExpressionComplexity)
506 {
507 infoSink.info << "Expression too complex.";
508 return false;
509 }
510
Jamie Madilleb1a0102013-07-08 13:31:38 -0400511 TDependencyGraph graph(root);
512
513 for (TFunctionCallVector::const_iterator iter = graph.beginUserDefinedFunctionCalls();
514 iter != graph.endUserDefinedFunctionCalls();
515 ++iter)
516 {
517 TGraphFunctionCall* samplerSymbol = *iter;
518 TDependencyGraphTraverser graphTraverser;
519 samplerSymbol->traverse(&graphTraverser);
520 }
521
Jamie Madilleb1a0102013-07-08 13:31:38 -0400522 return true;
523}
524
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000525bool TCompiler::enforceFragmentShaderTimingRestrictions(const TDependencyGraph& graph)
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000526{
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000527 RestrictFragmentShaderTiming restrictor(infoSink.info);
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000528 restrictor.enforceRestrictions(graph);
529 return restrictor.numErrors() == 0;
530}
531
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000532bool TCompiler::enforceVertexShaderTimingRestrictions(TIntermNode* root)
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000533{
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000534 RestrictVertexShaderTiming restrictor(infoSink.info);
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000535 restrictor.enforceRestrictions(root);
536 return restrictor.numErrors() == 0;
537}
538
Zhenyao Mo74da9f22013-09-23 14:57:01 -0400539void TCompiler::collectVariables(TIntermNode* root)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000540{
Jamie Madilla2fbb842014-09-03 09:40:47 -0400541 sh::CollectVariables collect(&attributes,
542 &outputVariables,
543 &uniforms,
544 &varyings,
545 &interfaceBlocks,
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700546 hashFunction,
547 symbolTable);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000548 root->traverse(&collect);
Jamie Madill23a8a432014-07-09 13:27:42 -0400549
Zhenyao Mo409078f2014-10-28 13:23:18 -0700550 // This is for enforcePackingRestriction().
551 sh::ExpandUniforms(uniforms, &expandedUniforms);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000552}
zmo@google.comfd747b82011-04-23 01:30:07 +0000553
gman@chromium.org8d804792012-10-17 21:33:48 +0000554bool TCompiler::enforcePackingRestrictions()
555{
556 VariablePacker packer;
Jamie Madill23a8a432014-07-09 13:27:42 -0400557 return packer.CheckVariablesWithinPackingLimits(maxUniformVectors, expandedUniforms);
gman@chromium.org8d804792012-10-17 21:33:48 +0000558}
559
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800560void TCompiler::initializeGLPosition(TIntermNode* root)
561{
562 InitializeVariables::InitVariableInfoList variables;
563 InitializeVariables::InitVariableInfo var(
564 "gl_Position", TType(EbtFloat, EbpUndefined, EvqPosition, 4));
565 variables.push_back(var);
566 InitializeVariables initializer(variables);
567 root->traverse(&initializer);
568}
569
570void TCompiler::initializeVaryingsWithoutStaticUse(TIntermNode* root)
571{
572 InitializeVariables::InitVariableInfoList variables;
573 for (size_t ii = 0; ii < varyings.size(); ++ii)
574 {
Jamie Madilla718c1e2014-07-02 15:31:22 -0400575 const sh::Varying& varying = varyings[ii];
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800576 if (varying.staticUse)
577 continue;
Jamie Madillaa72d782014-07-02 15:31:19 -0400578 unsigned char primarySize = static_cast<unsigned char>(gl::VariableColumnCount(varying.type));
579 unsigned char secondarySize = static_cast<unsigned char>(gl::VariableRowCount(varying.type));
Jamie Madilla718c1e2014-07-02 15:31:22 -0400580 TType type(EbtFloat, EbpUndefined, EvqVaryingOut, primarySize, secondarySize, varying.isArray());
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800581 TString name = varying.name.c_str();
Jamie Madilla718c1e2014-07-02 15:31:22 -0400582 if (varying.isArray())
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800583 {
Jamie Madilla718c1e2014-07-02 15:31:22 -0400584 type.setArraySize(varying.arraySize);
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800585 name = name.substr(0, name.find_first_of('['));
586 }
587
588 InitializeVariables::InitVariableInfo var(name, type);
589 variables.push_back(var);
590 }
591 InitializeVariables initializer(variables);
592 root->traverse(&initializer);
593}
594
zmo@google.com5601ea02011-06-10 18:23:25 +0000595const TExtensionBehavior& TCompiler::getExtensionBehavior() const
596{
597 return extensionBehavior;
598}
zmo@google.com32e97312011-08-24 01:03:11 +0000599
shannon.woods%transgaming.com@gtempaccount.com18b4c4b2013-04-13 03:31:40 +0000600const ShBuiltInResources& TCompiler::getResources() const
601{
602 return compileResources;
603}
604
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000605const ArrayBoundsClamper& TCompiler::getArrayBoundsClamper() const
606{
607 return arrayBoundsClamper;
608}
609
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000610ShArrayIndexClampingStrategy TCompiler::getArrayIndexClampingStrategy() const
611{
612 return clampingStrategy;
613}
614
615const BuiltInFunctionEmulator& TCompiler::getBuiltInFunctionEmulator() const
616{
617 return builtInFunctionEmulator;
618}
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700619
620void TCompiler::writePragma()
621{
622 TInfoSinkBase &sink = infoSink.obj;
623 if (mPragma.stdgl.invariantAll)
624 sink << "#pragma STDGL invariant(all)\n";
625}