blob: 5eac0c5826fc25038c81a634176cfdef794f5e69 [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
Jamie Madilld4a3a312014-06-25 16:04:56 -04007#include "compiler/translator/Compiler.h"
Geoff Lang17732822013-08-29 13:46:49 -04008#include "compiler/translator/DetectCallDepth.h"
9#include "compiler/translator/ForLoopUnroll.h"
10#include "compiler/translator/Initialize.h"
Geoff Lang17732822013-08-29 13:46:49 -040011#include "compiler/translator/InitializeParseContext.h"
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080012#include "compiler/translator/InitializeVariables.h"
Jamie Madill6b9cb252013-10-17 10:45:47 -040013#include "compiler/translator/ParseContext.h"
Zhenyao Moe740add2014-07-18 17:01:01 -070014#include "compiler/translator/RegenerateStructNames.h"
Geoff Lang17732822013-08-29 13:46:49 -040015#include "compiler/translator/RenameFunction.h"
Zhenyao Mocd68fe72014-07-11 10:45:44 -070016#include "compiler/translator/ScalarizeVecAndMatConstructorArgs.h"
Zhenyao Mo7cab38b2013-10-15 12:59:30 -070017#include "compiler/translator/UnfoldShortCircuitAST.h"
Geoff Lang17732822013-08-29 13:46:49 -040018#include "compiler/translator/ValidateLimitations.h"
19#include "compiler/translator/ValidateOutputs.h"
20#include "compiler/translator/VariablePacker.h"
21#include "compiler/translator/depgraph/DependencyGraph.h"
22#include "compiler/translator/depgraph/DependencyGraphOutput.h"
23#include "compiler/translator/timing/RestrictFragmentShaderTiming.h"
24#include "compiler/translator/timing/RestrictVertexShaderTiming.h"
shannon.woods@transgaming.comda1ed362013-01-25 21:54:57 +000025#include "third_party/compiler/ArrayBoundsClamper.h"
Jamie Madill183bde52014-07-02 15:31:19 -040026#include "angle_gl.h"
Jamie Madillaa72d782014-07-02 15:31:19 -040027#include "common/utilities.h"
alokp@chromium.org07620a52010-09-23 17:53:56 +000028
Jamie Madill5508f392014-02-20 13:31:36 -050029bool IsWebGLBasedSpec(ShShaderSpec spec)
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +000030{
Zhenyao Modb9b40b2014-10-29 15:00:04 -070031 return (spec == SH_WEBGL_SPEC ||
32 spec == SH_CSS_SHADERS_SPEC ||
33 spec == SH_WEBGL2_SPEC);
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +000034}
35
Zhenyao Mo7faf1a12014-04-25 18:03:56 -070036size_t GetGlobalMaxTokenSize(ShShaderSpec spec)
Jamie Madill88f6e942014-02-19 10:27:53 -050037{
Jamie Madill88f6e942014-02-19 10:27:53 -050038 // WebGL defines a max token legnth of 256, while ES2 leaves max token
39 // size undefined. ES3 defines a max size of 1024 characters.
Zhenyao Modb9b40b2014-10-29 15:00:04 -070040 switch (spec)
Jamie Madill88f6e942014-02-19 10:27:53 -050041 {
Zhenyao Modb9b40b2014-10-29 15:00:04 -070042 case SH_WEBGL_SPEC:
43 case SH_CSS_SHADERS_SPEC:
Jamie Madill88f6e942014-02-19 10:27:53 -050044 return 256;
Zhenyao Modb9b40b2014-10-29 15:00:04 -070045 default:
Jamie Madill88f6e942014-02-19 10:27:53 -050046 return 1024;
47 }
48}
49
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +000050namespace {
Zhenyao Modb9b40b2014-10-29 15:00:04 -070051
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080052class TScopedPoolAllocator
53{
54 public:
55 TScopedPoolAllocator(TPoolAllocator* allocator) : mAllocator(allocator)
56 {
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -040057 mAllocator->push();
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +000058 SetGlobalPoolAllocator(mAllocator);
59 }
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080060 ~TScopedPoolAllocator()
61 {
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +000062 SetGlobalPoolAllocator(NULL);
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -040063 mAllocator->pop();
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +000064 }
65
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080066 private:
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +000067 TPoolAllocator* mAllocator;
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -040068};
69
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080070class TScopedSymbolTableLevel
71{
72 public:
73 TScopedSymbolTableLevel(TSymbolTable* table) : mTable(table)
74 {
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -040075 ASSERT(mTable->atBuiltInLevel());
76 mTable->push();
77 }
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080078 ~TScopedSymbolTableLevel()
79 {
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -040080 while (!mTable->atBuiltInLevel())
81 mTable->pop();
82 }
83
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080084 private:
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -040085 TSymbolTable* mTable;
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +000086};
Zhenyao Modb9b40b2014-10-29 15:00:04 -070087
88int MapSpecToShaderVersion(ShShaderSpec spec)
89{
90 switch (spec)
91 {
92 case SH_GLES2_SPEC:
93 case SH_WEBGL_SPEC:
94 case SH_CSS_SHADERS_SPEC:
95 return 100;
96 case SH_GLES3_SPEC:
97 case SH_WEBGL2_SPEC:
98 return 300;
99 default:
100 UNREACHABLE();
101 return 0;
102 }
103}
104
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000105} // namespace
106
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800107TShHandleBase::TShHandleBase()
108{
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000109 allocator.push();
110 SetGlobalPoolAllocator(&allocator);
111}
112
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800113TShHandleBase::~TShHandleBase()
114{
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000115 SetGlobalPoolAllocator(NULL);
116 allocator.popAll();
117}
118
Jamie Madill183bde52014-07-02 15:31:19 -0400119TCompiler::TCompiler(sh::GLenum type, ShShaderSpec spec, ShShaderOutput output)
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000120 : shaderType(type),
zmo@google.comf420c422011-09-12 18:27:59 +0000121 shaderSpec(spec),
Jamie Madill68fe74a2014-05-27 12:56:01 -0400122 outputType(output),
Jamie Madilleb1a0102013-07-08 13:31:38 -0400123 maxUniformVectors(0),
124 maxExpressionComplexity(0),
125 maxCallStackDepth(0),
shannon.woods%transgaming.com@gtempaccount.comcbb6b6a2013-04-13 03:27:47 +0000126 fragmentPrecisionHigh(false),
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000127 clampingStrategy(SH_CLAMP_WITH_CLAMP_INTRINSIC),
zmo@google.com9996b8e2012-01-19 01:43:55 +0000128 builtInFunctionEmulator(type)
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000129{
130}
131
132TCompiler::~TCompiler()
133{
134}
135
136bool TCompiler::Init(const ShBuiltInResources& resources)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000137{
shannon.woods%transgaming.com@gtempaccount.com0bbed382013-04-13 03:38:07 +0000138 shaderVersion = 100;
Jamie Madill183bde52014-07-02 15:31:19 -0400139 maxUniformVectors = (shaderType == GL_VERTEX_SHADER) ?
gman@chromium.org8d804792012-10-17 21:33:48 +0000140 resources.MaxVertexUniformVectors :
141 resources.MaxFragmentUniformVectors;
Jamie Madilleb1a0102013-07-08 13:31:38 -0400142 maxExpressionComplexity = resources.MaxExpressionComplexity;
143 maxCallStackDepth = resources.MaxCallStackDepth;
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400144
145 SetGlobalPoolAllocator(&allocator);
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000146
alokp@chromium.org07620a52010-09-23 17:53:56 +0000147 // Generate built-in symbol table.
148 if (!InitBuiltInSymbolTable(resources))
149 return false;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000150 InitExtensionBehavior(resources, extensionBehavior);
shannon.woods%transgaming.com@gtempaccount.comcbb6b6a2013-04-13 03:27:47 +0000151 fragmentPrecisionHigh = resources.FragmentPrecisionHigh == 1;
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000152
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000153 arrayBoundsClamper.SetClampingStrategy(resources.ArrayIndexClampingStrategy);
154 clampingStrategy = resources.ArrayIndexClampingStrategy;
155
daniel@transgaming.comc23f4612012-11-28 19:42:57 +0000156 hashFunction = resources.HashFunction;
157
alokp@chromium.org07620a52010-09-23 17:53:56 +0000158 return true;
159}
160
161bool TCompiler::compile(const char* const shaderStrings[],
shannon.woods@transgaming.comd64b3da2013-02-28 23:19:26 +0000162 size_t numStrings,
alokp@chromium.org07620a52010-09-23 17:53:56 +0000163 int compileOptions)
164{
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400165 TScopedPoolAllocator scopedAlloc(&allocator);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000166 clearResults();
167
168 if (numStrings == 0)
169 return true;
170
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000171 // If compiling for WebGL, validate loop and indexing as well.
Jamie Madill5508f392014-02-20 13:31:36 -0500172 if (IsWebGLBasedSpec(shaderSpec))
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000173 compileOptions |= SH_VALIDATE_LOOP_INDEXING;
alokp@chromium.org1f299542010-11-12 15:50:23 +0000174
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000175 // First string is path of source file if flag is set. The actual source follows.
176 const char* sourcePath = NULL;
shannon.woods@transgaming.comd64b3da2013-02-28 23:19:26 +0000177 size_t firstSource = 0;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000178 if (compileOptions & SH_SOURCE_PATH)
179 {
180 sourcePath = shaderStrings[0];
181 ++firstSource;
182 }
183
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200184 bool debugShaderPrecision = getResources().WEBGL_debug_shader_precision == 1;
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,
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200188 sourcePath, infoSink, debugShaderPrecision);
189
shannon.woods%transgaming.com@gtempaccount.comcbb6b6a2013-04-13 03:27:47 +0000190 parseContext.fragmentPrecisionHigh = fragmentPrecisionHigh;
Alok Priyadarshi8156b6b2013-09-23 14:56:58 -0400191 SetGlobalParseContext(&parseContext);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000192
193 // We preserve symbols at the built-in level from compile-to-compile.
194 // Start pushing the user-defined symbols at global level.
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400195 TScopedSymbolTableLevel scopedSymbolLevel(&symbolTable);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000196
197 // Parse shader.
198 bool success =
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000199 (PaParseStrings(numStrings - firstSource, &shaderStrings[firstSource], NULL, &parseContext) == 0) &&
alokp@chromium.org07620a52010-09-23 17:53:56 +0000200 (parseContext.treeRoot != NULL);
shannon.woods%transgaming.com@gtempaccount.com0bbed382013-04-13 03:38:07 +0000201
shannon.woods%transgaming.com@gtempaccount.com5524db02013-04-13 03:38:16 +0000202 shaderVersion = parseContext.getShaderVersion();
Zhenyao Modb9b40b2014-10-29 15:00:04 -0700203 if (success && MapSpecToShaderVersion(shaderSpec) < shaderVersion)
204 {
205 infoSink.info.prefix(EPrefixError);
206 infoSink.info << "unsupported shader version";
207 success = false;
208 }
shannon.woods%transgaming.com@gtempaccount.com0bbed382013-04-13 03:38:07 +0000209
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800210 if (success)
211 {
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700212 mPragma = parseContext.pragma();
213 if (mPragma.stdgl.invariantAll)
214 {
215 symbolTable.setGlobalInvariant();
216 }
217
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000218 TIntermNode* root = parseContext.treeRoot;
219 success = intermediate.postProcess(root);
220
Jamie Madill6654bc92014-03-26 14:01:57 -0400221 // Disallow expressions deemed too complex.
222 if (success && (compileOptions & SH_LIMIT_EXPRESSION_COMPLEXITY))
223 success = limitExpressionComplexity(root);
224
zmo@google.comb1762df2011-07-30 02:04:23 +0000225 if (success)
Jamie Madilleb1a0102013-07-08 13:31:38 -0400226 success = detectCallDepth(root, infoSink, (compileOptions & SH_LIMIT_CALL_STACK_DEPTH) != 0);
zmo@google.comb1762df2011-07-30 02:04:23 +0000227
Jamie Madill183bde52014-07-02 15:31:19 -0400228 if (success && shaderVersion == 300 && shaderType == GL_FRAGMENT_SHADER)
Jamie Madill05a80ce2013-06-20 11:55:49 -0400229 success = validateOutputs(root);
230
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000231 if (success && (compileOptions & SH_VALIDATE_LOOP_INDEXING))
232 success = validateLimitations(root);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000233
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000234 if (success && (compileOptions & SH_TIMING_RESTRICTIONS))
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000235 success = enforceTimingRestrictions(root, (compileOptions & SH_DEPENDENCY_GRAPH) != 0);
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000236
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +0000237 if (success && shaderSpec == SH_CSS_SHADERS_SPEC)
238 rewriteCSSShader(root);
239
zmo@google.com0c6bb7a2011-08-17 19:39:58 +0000240 // Unroll for-loop markup needs to happen after validateLimitations pass.
241 if (success && (compileOptions & SH_UNROLL_FOR_LOOP_WITH_INTEGER_INDEX))
Zhenyao Mo3cdfcce2014-03-07 13:00:08 -0800242 {
243 ForLoopUnrollMarker marker(ForLoopUnrollMarker::kIntegerIndex);
Zhenyao Mo550c6002014-02-26 15:40:48 -0800244 root->traverse(&marker);
245 }
246 if (success && (compileOptions & SH_UNROLL_FOR_LOOP_WITH_SAMPLER_ARRAY_INDEX))
Zhenyao Mo3cdfcce2014-03-07 13:00:08 -0800247 {
248 ForLoopUnrollMarker marker(ForLoopUnrollMarker::kSamplerArrayIndex);
Zhenyao Mo550c6002014-02-26 15:40:48 -0800249 root->traverse(&marker);
250 if (marker.samplerArrayIndexIsFloatLoopIndex())
251 {
252 infoSink.info.prefix(EPrefixError);
253 infoSink.info << "sampler array index is float loop index";
254 success = false;
255 }
256 }
zmo@google.com0c6bb7a2011-08-17 19:39:58 +0000257
zmo@google.com32e97312011-08-24 01:03:11 +0000258 // Built-in function emulation needs to happen after validateLimitations pass.
259 if (success && (compileOptions & SH_EMULATE_BUILT_IN_FUNCTIONS))
260 builtInFunctionEmulator.MarkBuiltInFunctionsForEmulation(root);
261
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000262 // Clamping uniform array bounds needs to happen after validateLimitations pass.
263 if (success && (compileOptions & SH_CLAMP_INDIRECT_ARRAY_BOUNDS))
264 arrayBoundsClamper.MarkIndirectArrayBoundsForClamping(root);
265
Jamie Madill183bde52014-07-02 15:31:19 -0400266 if (success && shaderType == GL_VERTEX_SHADER && (compileOptions & SH_INIT_GL_POSITION))
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800267 initializeGLPosition(root);
Zhenyao Moac44cd22013-09-23 14:57:09 -0400268
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800269 if (success && (compileOptions & SH_UNFOLD_SHORT_CIRCUIT))
270 {
Zhenyao Mo7cab38b2013-10-15 12:59:30 -0700271 UnfoldShortCircuitAST unfoldShortCircuit;
272 root->traverse(&unfoldShortCircuit);
273 unfoldShortCircuit.updateTree();
274 }
275
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800276 if (success && (compileOptions & SH_VARIABLES))
277 {
Zhenyao Mo74da9f22013-09-23 14:57:01 -0400278 collectVariables(root);
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800279 if (compileOptions & SH_ENFORCE_PACKING_RESTRICTIONS)
280 {
gman@chromium.org8d804792012-10-17 21:33:48 +0000281 success = enforcePackingRestrictions();
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800282 if (!success)
283 {
Jamie Madill075edd82013-07-08 13:30:19 -0400284 infoSink.info.prefix(EPrefixError);
285 infoSink.info << "too many uniforms";
gman@chromium.org8d804792012-10-17 21:33:48 +0000286 }
287 }
Jamie Madill183bde52014-07-02 15:31:19 -0400288 if (success && shaderType == GL_VERTEX_SHADER &&
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800289 (compileOptions & SH_INIT_VARYINGS_WITHOUT_STATIC_USE))
290 initializeVaryingsWithoutStaticUse(root);
gman@chromium.org8d804792012-10-17 21:33:48 +0000291 }
zmo@google.comfd747b82011-04-23 01:30:07 +0000292
Zhenyao Mocd68fe72014-07-11 10:45:44 -0700293 if (success && (compileOptions & SH_SCALARIZE_VEC_AND_MAT_CONSTRUCTOR_ARGS))
294 {
Zhenyao Modaf56572014-08-06 16:18:30 -0700295 ScalarizeVecAndMatConstructorArgs scalarizer(
296 shaderType, fragmentPrecisionHigh);
Zhenyao Mocd68fe72014-07-11 10:45:44 -0700297 root->traverse(&scalarizer);
298 }
299
Zhenyao Moe740add2014-07-18 17:01:01 -0700300 if (success && (compileOptions & SH_REGENERATE_STRUCT_NAMES))
301 {
302 RegenerateStructNames gen(symbolTable, shaderVersion);
303 root->traverse(&gen);
304 }
305
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000306 if (success && (compileOptions & SH_INTERMEDIATE_TREE))
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000307 intermediate.outputTree(root);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000308
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000309 if (success && (compileOptions & SH_OBJECT_CODE))
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000310 translate(root);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000311 }
312
Olli Etuaho352beff2014-11-19 13:45:55 +0200313 // Cleanup. The IntermNode tree doesn't need to be deleted here, since the
314 // memory will be freed in a big chunk by the PoolAllocator.
Zhenyao Mo7faf1a12014-04-25 18:03:56 -0700315 SetGlobalParseContext(NULL);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000316 return success;
317}
318
Nicolas Capens49a88872013-06-20 09:54:03 -0400319bool TCompiler::InitBuiltInSymbolTable(const ShBuiltInResources &resources)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000320{
shannon.woods%transgaming.com@gtempaccount.com18b4c4b2013-04-13 03:31:40 +0000321 compileResources = resources;
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400322 setResourceString();
shannonwoods@chromium.org2ac0be92013-05-30 00:02:27 +0000323
Nicolas Capens49a88872013-06-20 09:54:03 -0400324 assert(symbolTable.isEmpty());
325 symbolTable.push(); // COMMON_BUILTINS
326 symbolTable.push(); // ESSL1_BUILTINS
327 symbolTable.push(); // ESSL3_BUILTINS
shannonwoods@chromium.org2ac0be92013-05-30 00:02:27 +0000328
Nicolas Capens49a88872013-06-20 09:54:03 -0400329 TPublicType integer;
330 integer.type = EbtInt;
331 integer.primarySize = 1;
332 integer.secondarySize = 1;
333 integer.array = false;
334
335 TPublicType floatingPoint;
336 floatingPoint.type = EbtFloat;
337 floatingPoint.primarySize = 1;
338 floatingPoint.secondarySize = 1;
339 floatingPoint.array = false;
340
Zhenyao Moa5a1dfc2013-09-23 14:57:03 -0400341 TPublicType sampler;
342 sampler.primarySize = 1;
343 sampler.secondarySize = 1;
344 sampler.array = false;
345
Nicolas Capens49a88872013-06-20 09:54:03 -0400346 switch(shaderType)
347 {
Jamie Madill183bde52014-07-02 15:31:19 -0400348 case GL_FRAGMENT_SHADER:
Nicolas Capens49a88872013-06-20 09:54:03 -0400349 symbolTable.setDefaultPrecision(integer, EbpMedium);
350 break;
Jamie Madill183bde52014-07-02 15:31:19 -0400351 case GL_VERTEX_SHADER:
Nicolas Capens49a88872013-06-20 09:54:03 -0400352 symbolTable.setDefaultPrecision(integer, EbpHigh);
353 symbolTable.setDefaultPrecision(floatingPoint, EbpHigh);
354 break;
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800355 default:
356 assert(false && "Language not supported");
Nicolas Capens49a88872013-06-20 09:54:03 -0400357 }
Zhenyao Moa5a1dfc2013-09-23 14:57:03 -0400358 // We set defaults for all the sampler types, even those that are
359 // only available if an extension exists.
360 for (int samplerType = EbtGuardSamplerBegin + 1;
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800361 samplerType < EbtGuardSamplerEnd; ++samplerType)
362 {
Zhenyao Moa5a1dfc2013-09-23 14:57:03 -0400363 sampler.type = static_cast<TBasicType>(samplerType);
364 symbolTable.setDefaultPrecision(sampler, EbpLow);
365 }
Nicolas Capens49a88872013-06-20 09:54:03 -0400366
Jamie Madill1b452142013-07-12 14:51:11 -0400367 InsertBuiltInFunctions(shaderType, shaderSpec, resources, symbolTable);
Nicolas Capens49a88872013-06-20 09:54:03 -0400368
369 IdentifyBuiltIns(shaderType, shaderSpec, resources, symbolTable);
370
371 return true;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000372}
373
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400374void TCompiler::setResourceString()
375{
376 std::ostringstream strstream;
377 strstream << ":MaxVertexAttribs:" << compileResources.MaxVertexAttribs
378 << ":MaxVertexUniformVectors:" << compileResources.MaxVertexUniformVectors
379 << ":MaxVaryingVectors:" << compileResources.MaxVaryingVectors
380 << ":MaxVertexTextureImageUnits:" << compileResources.MaxVertexTextureImageUnits
381 << ":MaxCombinedTextureImageUnits:" << compileResources.MaxCombinedTextureImageUnits
382 << ":MaxTextureImageUnits:" << compileResources.MaxTextureImageUnits
383 << ":MaxFragmentUniformVectors:" << compileResources.MaxFragmentUniformVectors
384 << ":MaxDrawBuffers:" << compileResources.MaxDrawBuffers
385 << ":OES_standard_derivatives:" << compileResources.OES_standard_derivatives
386 << ":OES_EGL_image_external:" << compileResources.OES_EGL_image_external
387 << ":ARB_texture_rectangle:" << compileResources.ARB_texture_rectangle
388 << ":EXT_draw_buffers:" << compileResources.EXT_draw_buffers
389 << ":FragmentPrecisionHigh:" << compileResources.FragmentPrecisionHigh
390 << ":MaxExpressionComplexity:" << compileResources.MaxExpressionComplexity
391 << ":MaxCallStackDepth:" << compileResources.MaxCallStackDepth
392 << ":EXT_frag_depth:" << compileResources.EXT_frag_depth
393 << ":EXT_shader_texture_lod:" << compileResources.EXT_shader_texture_lod
Erik Dahlströmea7a2122014-11-17 16:15:57 +0100394 << ":EXT_shader_framebuffer_fetch:" << compileResources.EXT_shader_framebuffer_fetch
395 << ":NV_shader_framebuffer_fetch:" << compileResources.NV_shader_framebuffer_fetch
396 << ":ARM_shader_framebuffer_fetch:" << compileResources.ARM_shader_framebuffer_fetch
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400397 << ":MaxVertexOutputVectors:" << compileResources.MaxVertexOutputVectors
398 << ":MaxFragmentInputVectors:" << compileResources.MaxFragmentInputVectors
399 << ":MinProgramTexelOffset:" << compileResources.MinProgramTexelOffset
Olli Etuahoe61209a2014-09-26 12:01:17 +0300400 << ":MaxProgramTexelOffset:" << compileResources.MaxProgramTexelOffset
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200401 << ":NV_draw_buffers:" << compileResources.NV_draw_buffers
402 << ":WEBGL_debug_shader_precision:" << compileResources.WEBGL_debug_shader_precision;
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400403
404 builtInResourcesString = strstream.str();
405}
406
alokp@chromium.org07620a52010-09-23 17:53:56 +0000407void TCompiler::clearResults()
408{
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000409 arrayBoundsClamper.Cleanup();
alokp@chromium.org07620a52010-09-23 17:53:56 +0000410 infoSink.info.erase();
411 infoSink.obj.erase();
412 infoSink.debug.erase();
413
Jamie Madilled27c722014-07-02 15:31:23 -0400414 attributes.clear();
415 outputVariables.clear();
alokp@chromium.org07620a52010-09-23 17:53:56 +0000416 uniforms.clear();
Jamie Madill23a8a432014-07-09 13:27:42 -0400417 expandedUniforms.clear();
Zhenyao Mod2d340b2013-09-23 14:57:05 -0400418 varyings.clear();
Jamie Madilled27c722014-07-02 15:31:23 -0400419 interfaceBlocks.clear();
zmo@google.coma3b4ab42011-09-16 00:53:26 +0000420
421 builtInFunctionEmulator.Cleanup();
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +0000422
423 nameMap.clear();
alokp@chromium.org07620a52010-09-23 17:53:56 +0000424}
425
Austin Kinross3ae64652015-01-26 15:51:39 -0800426bool TCompiler::detectCallDepth(TIntermNode* inputRoot, TInfoSink& inputInfoSink, bool limitCallStackDepth)
zmo@google.comb1762df2011-07-30 02:04:23 +0000427{
Austin Kinross3ae64652015-01-26 15:51:39 -0800428 DetectCallDepth detect(inputInfoSink, limitCallStackDepth, maxCallStackDepth);
429 inputRoot->traverse(&detect);
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800430 switch (detect.detectCallDepth())
431 {
432 case DetectCallDepth::kErrorNone:
433 return true;
434 case DetectCallDepth::kErrorMissingMain:
Austin Kinross3ae64652015-01-26 15:51:39 -0800435 inputInfoSink.info.prefix(EPrefixError);
436 inputInfoSink.info << "Missing main()";
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800437 return false;
438 case DetectCallDepth::kErrorRecursion:
Austin Kinross3ae64652015-01-26 15:51:39 -0800439 inputInfoSink.info.prefix(EPrefixError);
440 inputInfoSink.info << "Function recursion detected";
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800441 return false;
442 case DetectCallDepth::kErrorMaxDepthExceeded:
Austin Kinross3ae64652015-01-26 15:51:39 -0800443 inputInfoSink.info.prefix(EPrefixError);
444 inputInfoSink.info << "Function call stack too deep";
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800445 return false;
446 default:
447 UNREACHABLE();
448 return false;
zmo@google.comb1762df2011-07-30 02:04:23 +0000449 }
450}
451
Jamie Madill05a80ce2013-06-20 11:55:49 -0400452bool TCompiler::validateOutputs(TIntermNode* root)
453{
454 ValidateOutputs validateOutputs(infoSink.info, compileResources.MaxDrawBuffers);
455 root->traverse(&validateOutputs);
456 return (validateOutputs.numErrors() == 0);
457}
458
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +0000459void TCompiler::rewriteCSSShader(TIntermNode* root)
460{
461 RenameFunction renamer("main(", "css_main(");
462 root->traverse(&renamer);
463}
464
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800465bool TCompiler::validateLimitations(TIntermNode* root)
466{
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000467 ValidateLimitations validate(shaderType, infoSink.info);
468 root->traverse(&validate);
469 return validate.numErrors() == 0;
470}
471
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000472bool TCompiler::enforceTimingRestrictions(TIntermNode* root, bool outputGraph)
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000473{
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800474 if (shaderSpec != SH_WEBGL_SPEC)
475 {
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000476 infoSink.info << "Timing restrictions must be enforced under the WebGL spec.";
477 return false;
478 }
479
Jamie Madill183bde52014-07-02 15:31:19 -0400480 if (shaderType == GL_FRAGMENT_SHADER)
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800481 {
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000482 TDependencyGraph graph(root);
483
484 // Output any errors first.
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000485 bool success = enforceFragmentShaderTimingRestrictions(graph);
Jamie Madill5508f392014-02-20 13:31:36 -0500486
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000487 // Then, output the dependency graph.
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800488 if (outputGraph)
489 {
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000490 TDependencyGraphOutput output(infoSink.info);
491 output.outputAllSpanningTrees(graph);
492 }
Jamie Madill5508f392014-02-20 13:31:36 -0500493
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000494 return success;
495 }
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800496 else
497 {
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000498 return enforceVertexShaderTimingRestrictions(root);
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000499 }
500}
501
Jamie Madilleb1a0102013-07-08 13:31:38 -0400502bool TCompiler::limitExpressionComplexity(TIntermNode* root)
503{
Jamie Madill6654bc92014-03-26 14:01:57 -0400504 TMaxDepthTraverser traverser(maxExpressionComplexity+1);
Jamie Madilleb1a0102013-07-08 13:31:38 -0400505 root->traverse(&traverser);
Jamie Madill6654bc92014-03-26 14:01:57 -0400506
507 if (traverser.getMaxDepth() > maxExpressionComplexity)
508 {
509 infoSink.info << "Expression too complex.";
510 return false;
511 }
512
Jamie Madilleb1a0102013-07-08 13:31:38 -0400513 TDependencyGraph graph(root);
514
515 for (TFunctionCallVector::const_iterator iter = graph.beginUserDefinedFunctionCalls();
516 iter != graph.endUserDefinedFunctionCalls();
517 ++iter)
518 {
519 TGraphFunctionCall* samplerSymbol = *iter;
520 TDependencyGraphTraverser graphTraverser;
521 samplerSymbol->traverse(&graphTraverser);
522 }
523
Jamie Madilleb1a0102013-07-08 13:31:38 -0400524 return true;
525}
526
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000527bool TCompiler::enforceFragmentShaderTimingRestrictions(const TDependencyGraph& graph)
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000528{
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000529 RestrictFragmentShaderTiming restrictor(infoSink.info);
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000530 restrictor.enforceRestrictions(graph);
531 return restrictor.numErrors() == 0;
532}
533
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000534bool TCompiler::enforceVertexShaderTimingRestrictions(TIntermNode* root)
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000535{
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000536 RestrictVertexShaderTiming restrictor(infoSink.info);
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000537 restrictor.enforceRestrictions(root);
538 return restrictor.numErrors() == 0;
539}
540
Zhenyao Mo74da9f22013-09-23 14:57:01 -0400541void TCompiler::collectVariables(TIntermNode* root)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000542{
Jamie Madilla2fbb842014-09-03 09:40:47 -0400543 sh::CollectVariables collect(&attributes,
544 &outputVariables,
545 &uniforms,
546 &varyings,
547 &interfaceBlocks,
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700548 hashFunction,
549 symbolTable);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000550 root->traverse(&collect);
Jamie Madill23a8a432014-07-09 13:27:42 -0400551
Zhenyao Mo409078f2014-10-28 13:23:18 -0700552 // This is for enforcePackingRestriction().
553 sh::ExpandUniforms(uniforms, &expandedUniforms);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000554}
zmo@google.comfd747b82011-04-23 01:30:07 +0000555
gman@chromium.org8d804792012-10-17 21:33:48 +0000556bool TCompiler::enforcePackingRestrictions()
557{
558 VariablePacker packer;
Jamie Madill23a8a432014-07-09 13:27:42 -0400559 return packer.CheckVariablesWithinPackingLimits(maxUniformVectors, expandedUniforms);
gman@chromium.org8d804792012-10-17 21:33:48 +0000560}
561
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800562void TCompiler::initializeGLPosition(TIntermNode* root)
563{
564 InitializeVariables::InitVariableInfoList variables;
565 InitializeVariables::InitVariableInfo var(
566 "gl_Position", TType(EbtFloat, EbpUndefined, EvqPosition, 4));
567 variables.push_back(var);
568 InitializeVariables initializer(variables);
569 root->traverse(&initializer);
570}
571
572void TCompiler::initializeVaryingsWithoutStaticUse(TIntermNode* root)
573{
574 InitializeVariables::InitVariableInfoList variables;
575 for (size_t ii = 0; ii < varyings.size(); ++ii)
576 {
Jamie Madilla718c1e2014-07-02 15:31:22 -0400577 const sh::Varying& varying = varyings[ii];
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800578 if (varying.staticUse)
579 continue;
Jamie Madillaa72d782014-07-02 15:31:19 -0400580 unsigned char primarySize = static_cast<unsigned char>(gl::VariableColumnCount(varying.type));
581 unsigned char secondarySize = static_cast<unsigned char>(gl::VariableRowCount(varying.type));
Jamie Madilla718c1e2014-07-02 15:31:22 -0400582 TType type(EbtFloat, EbpUndefined, EvqVaryingOut, primarySize, secondarySize, varying.isArray());
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800583 TString name = varying.name.c_str();
Jamie Madilla718c1e2014-07-02 15:31:22 -0400584 if (varying.isArray())
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800585 {
Jamie Madilla718c1e2014-07-02 15:31:22 -0400586 type.setArraySize(varying.arraySize);
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800587 name = name.substr(0, name.find_first_of('['));
588 }
589
590 InitializeVariables::InitVariableInfo var(name, type);
591 variables.push_back(var);
592 }
593 InitializeVariables initializer(variables);
594 root->traverse(&initializer);
595}
596
zmo@google.com5601ea02011-06-10 18:23:25 +0000597const TExtensionBehavior& TCompiler::getExtensionBehavior() const
598{
599 return extensionBehavior;
600}
zmo@google.com32e97312011-08-24 01:03:11 +0000601
shannon.woods%transgaming.com@gtempaccount.com18b4c4b2013-04-13 03:31:40 +0000602const ShBuiltInResources& TCompiler::getResources() const
603{
604 return compileResources;
605}
606
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000607const ArrayBoundsClamper& TCompiler::getArrayBoundsClamper() const
608{
609 return arrayBoundsClamper;
610}
611
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000612ShArrayIndexClampingStrategy TCompiler::getArrayIndexClampingStrategy() const
613{
614 return clampingStrategy;
615}
616
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +0200617const BuiltInFunctionEmulatorGLSL& TCompiler::getBuiltInFunctionEmulator() const
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000618{
619 return builtInFunctionEmulator;
620}
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700621
622void TCompiler::writePragma()
623{
624 TInfoSinkBase &sink = infoSink.obj;
625 if (mPragma.stdgl.invariantAll)
626 sink << "#pragma STDGL invariant(all)\n";
627}