blob: 9e64bcb3514254f0dec5e7b5c521ff0896bde966 [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
David Yen0fbd1282015-02-02 14:46:09 -0800171 // Reset the extension behavior for each compilation unit.
172 ResetExtensionBehavior(extensionBehavior);
173
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000174 // If compiling for WebGL, validate loop and indexing as well.
Jamie Madill5508f392014-02-20 13:31:36 -0500175 if (IsWebGLBasedSpec(shaderSpec))
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000176 compileOptions |= SH_VALIDATE_LOOP_INDEXING;
alokp@chromium.org1f299542010-11-12 15:50:23 +0000177
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000178 // First string is path of source file if flag is set. The actual source follows.
179 const char* sourcePath = NULL;
shannon.woods@transgaming.comd64b3da2013-02-28 23:19:26 +0000180 size_t firstSource = 0;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000181 if (compileOptions & SH_SOURCE_PATH)
182 {
183 sourcePath = shaderStrings[0];
184 ++firstSource;
185 }
186
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200187 bool debugShaderPrecision = getResources().WEBGL_debug_shader_precision == 1;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000188 TIntermediate intermediate(infoSink);
189 TParseContext parseContext(symbolTable, extensionBehavior, intermediate,
zmo@google.comdc4b4f82011-06-17 00:42:53 +0000190 shaderType, shaderSpec, compileOptions, true,
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200191 sourcePath, infoSink, debugShaderPrecision);
192
shannon.woods%transgaming.com@gtempaccount.comcbb6b6a2013-04-13 03:27:47 +0000193 parseContext.fragmentPrecisionHigh = fragmentPrecisionHigh;
Alok Priyadarshi8156b6b2013-09-23 14:56:58 -0400194 SetGlobalParseContext(&parseContext);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000195
196 // We preserve symbols at the built-in level from compile-to-compile.
197 // Start pushing the user-defined symbols at global level.
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400198 TScopedSymbolTableLevel scopedSymbolLevel(&symbolTable);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000199
200 // Parse shader.
201 bool success =
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000202 (PaParseStrings(numStrings - firstSource, &shaderStrings[firstSource], NULL, &parseContext) == 0) &&
alokp@chromium.org07620a52010-09-23 17:53:56 +0000203 (parseContext.treeRoot != NULL);
shannon.woods%transgaming.com@gtempaccount.com0bbed382013-04-13 03:38:07 +0000204
shannon.woods%transgaming.com@gtempaccount.com5524db02013-04-13 03:38:16 +0000205 shaderVersion = parseContext.getShaderVersion();
Zhenyao Modb9b40b2014-10-29 15:00:04 -0700206 if (success && MapSpecToShaderVersion(shaderSpec) < shaderVersion)
207 {
208 infoSink.info.prefix(EPrefixError);
209 infoSink.info << "unsupported shader version";
210 success = false;
211 }
shannon.woods%transgaming.com@gtempaccount.com0bbed382013-04-13 03:38:07 +0000212
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800213 if (success)
214 {
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700215 mPragma = parseContext.pragma();
216 if (mPragma.stdgl.invariantAll)
217 {
218 symbolTable.setGlobalInvariant();
219 }
220
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000221 TIntermNode* root = parseContext.treeRoot;
222 success = intermediate.postProcess(root);
223
Jamie Madill6654bc92014-03-26 14:01:57 -0400224 // Disallow expressions deemed too complex.
225 if (success && (compileOptions & SH_LIMIT_EXPRESSION_COMPLEXITY))
226 success = limitExpressionComplexity(root);
227
zmo@google.comb1762df2011-07-30 02:04:23 +0000228 if (success)
Jamie Madilleb1a0102013-07-08 13:31:38 -0400229 success = detectCallDepth(root, infoSink, (compileOptions & SH_LIMIT_CALL_STACK_DEPTH) != 0);
zmo@google.comb1762df2011-07-30 02:04:23 +0000230
Jamie Madill183bde52014-07-02 15:31:19 -0400231 if (success && shaderVersion == 300 && shaderType == GL_FRAGMENT_SHADER)
Jamie Madill05a80ce2013-06-20 11:55:49 -0400232 success = validateOutputs(root);
233
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000234 if (success && (compileOptions & SH_VALIDATE_LOOP_INDEXING))
235 success = validateLimitations(root);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000236
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000237 if (success && (compileOptions & SH_TIMING_RESTRICTIONS))
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000238 success = enforceTimingRestrictions(root, (compileOptions & SH_DEPENDENCY_GRAPH) != 0);
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000239
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +0000240 if (success && shaderSpec == SH_CSS_SHADERS_SPEC)
241 rewriteCSSShader(root);
242
zmo@google.com0c6bb7a2011-08-17 19:39:58 +0000243 // Unroll for-loop markup needs to happen after validateLimitations pass.
244 if (success && (compileOptions & SH_UNROLL_FOR_LOOP_WITH_INTEGER_INDEX))
Zhenyao Mo3cdfcce2014-03-07 13:00:08 -0800245 {
246 ForLoopUnrollMarker marker(ForLoopUnrollMarker::kIntegerIndex);
Zhenyao Mo550c6002014-02-26 15:40:48 -0800247 root->traverse(&marker);
248 }
249 if (success && (compileOptions & SH_UNROLL_FOR_LOOP_WITH_SAMPLER_ARRAY_INDEX))
Zhenyao Mo3cdfcce2014-03-07 13:00:08 -0800250 {
251 ForLoopUnrollMarker marker(ForLoopUnrollMarker::kSamplerArrayIndex);
Zhenyao Mo550c6002014-02-26 15:40:48 -0800252 root->traverse(&marker);
253 if (marker.samplerArrayIndexIsFloatLoopIndex())
254 {
255 infoSink.info.prefix(EPrefixError);
256 infoSink.info << "sampler array index is float loop index";
257 success = false;
258 }
259 }
zmo@google.com0c6bb7a2011-08-17 19:39:58 +0000260
zmo@google.com32e97312011-08-24 01:03:11 +0000261 // Built-in function emulation needs to happen after validateLimitations pass.
262 if (success && (compileOptions & SH_EMULATE_BUILT_IN_FUNCTIONS))
263 builtInFunctionEmulator.MarkBuiltInFunctionsForEmulation(root);
264
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000265 // Clamping uniform array bounds needs to happen after validateLimitations pass.
266 if (success && (compileOptions & SH_CLAMP_INDIRECT_ARRAY_BOUNDS))
267 arrayBoundsClamper.MarkIndirectArrayBoundsForClamping(root);
268
Jamie Madill183bde52014-07-02 15:31:19 -0400269 if (success && shaderType == GL_VERTEX_SHADER && (compileOptions & SH_INIT_GL_POSITION))
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800270 initializeGLPosition(root);
Zhenyao Moac44cd22013-09-23 14:57:09 -0400271
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800272 if (success && (compileOptions & SH_UNFOLD_SHORT_CIRCUIT))
273 {
Zhenyao Mo7cab38b2013-10-15 12:59:30 -0700274 UnfoldShortCircuitAST unfoldShortCircuit;
275 root->traverse(&unfoldShortCircuit);
276 unfoldShortCircuit.updateTree();
277 }
278
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800279 if (success && (compileOptions & SH_VARIABLES))
280 {
Zhenyao Mo74da9f22013-09-23 14:57:01 -0400281 collectVariables(root);
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800282 if (compileOptions & SH_ENFORCE_PACKING_RESTRICTIONS)
283 {
gman@chromium.org8d804792012-10-17 21:33:48 +0000284 success = enforcePackingRestrictions();
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800285 if (!success)
286 {
Jamie Madill075edd82013-07-08 13:30:19 -0400287 infoSink.info.prefix(EPrefixError);
288 infoSink.info << "too many uniforms";
gman@chromium.org8d804792012-10-17 21:33:48 +0000289 }
290 }
Jamie Madill183bde52014-07-02 15:31:19 -0400291 if (success && shaderType == GL_VERTEX_SHADER &&
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800292 (compileOptions & SH_INIT_VARYINGS_WITHOUT_STATIC_USE))
293 initializeVaryingsWithoutStaticUse(root);
gman@chromium.org8d804792012-10-17 21:33:48 +0000294 }
zmo@google.comfd747b82011-04-23 01:30:07 +0000295
Zhenyao Mocd68fe72014-07-11 10:45:44 -0700296 if (success && (compileOptions & SH_SCALARIZE_VEC_AND_MAT_CONSTRUCTOR_ARGS))
297 {
Zhenyao Modaf56572014-08-06 16:18:30 -0700298 ScalarizeVecAndMatConstructorArgs scalarizer(
299 shaderType, fragmentPrecisionHigh);
Zhenyao Mocd68fe72014-07-11 10:45:44 -0700300 root->traverse(&scalarizer);
301 }
302
Zhenyao Moe740add2014-07-18 17:01:01 -0700303 if (success && (compileOptions & SH_REGENERATE_STRUCT_NAMES))
304 {
305 RegenerateStructNames gen(symbolTable, shaderVersion);
306 root->traverse(&gen);
307 }
308
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000309 if (success && (compileOptions & SH_INTERMEDIATE_TREE))
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000310 intermediate.outputTree(root);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000311
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000312 if (success && (compileOptions & SH_OBJECT_CODE))
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000313 translate(root);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000314 }
315
Olli Etuaho352beff2014-11-19 13:45:55 +0200316 // Cleanup. The IntermNode tree doesn't need to be deleted here, since the
317 // memory will be freed in a big chunk by the PoolAllocator.
Zhenyao Mo7faf1a12014-04-25 18:03:56 -0700318 SetGlobalParseContext(NULL);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000319 return success;
320}
321
Nicolas Capens49a88872013-06-20 09:54:03 -0400322bool TCompiler::InitBuiltInSymbolTable(const ShBuiltInResources &resources)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000323{
shannon.woods%transgaming.com@gtempaccount.com18b4c4b2013-04-13 03:31:40 +0000324 compileResources = resources;
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400325 setResourceString();
shannonwoods@chromium.org2ac0be92013-05-30 00:02:27 +0000326
Nicolas Capens49a88872013-06-20 09:54:03 -0400327 assert(symbolTable.isEmpty());
328 symbolTable.push(); // COMMON_BUILTINS
329 symbolTable.push(); // ESSL1_BUILTINS
330 symbolTable.push(); // ESSL3_BUILTINS
shannonwoods@chromium.org2ac0be92013-05-30 00:02:27 +0000331
Nicolas Capens49a88872013-06-20 09:54:03 -0400332 TPublicType integer;
333 integer.type = EbtInt;
334 integer.primarySize = 1;
335 integer.secondarySize = 1;
336 integer.array = false;
337
338 TPublicType floatingPoint;
339 floatingPoint.type = EbtFloat;
340 floatingPoint.primarySize = 1;
341 floatingPoint.secondarySize = 1;
342 floatingPoint.array = false;
343
Zhenyao Moa5a1dfc2013-09-23 14:57:03 -0400344 TPublicType sampler;
345 sampler.primarySize = 1;
346 sampler.secondarySize = 1;
347 sampler.array = false;
348
Nicolas Capens49a88872013-06-20 09:54:03 -0400349 switch(shaderType)
350 {
Jamie Madill183bde52014-07-02 15:31:19 -0400351 case GL_FRAGMENT_SHADER:
Nicolas Capens49a88872013-06-20 09:54:03 -0400352 symbolTable.setDefaultPrecision(integer, EbpMedium);
353 break;
Jamie Madill183bde52014-07-02 15:31:19 -0400354 case GL_VERTEX_SHADER:
Nicolas Capens49a88872013-06-20 09:54:03 -0400355 symbolTable.setDefaultPrecision(integer, EbpHigh);
356 symbolTable.setDefaultPrecision(floatingPoint, EbpHigh);
357 break;
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800358 default:
359 assert(false && "Language not supported");
Nicolas Capens49a88872013-06-20 09:54:03 -0400360 }
Zhenyao Moa5a1dfc2013-09-23 14:57:03 -0400361 // We set defaults for all the sampler types, even those that are
362 // only available if an extension exists.
363 for (int samplerType = EbtGuardSamplerBegin + 1;
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800364 samplerType < EbtGuardSamplerEnd; ++samplerType)
365 {
Zhenyao Moa5a1dfc2013-09-23 14:57:03 -0400366 sampler.type = static_cast<TBasicType>(samplerType);
367 symbolTable.setDefaultPrecision(sampler, EbpLow);
368 }
Nicolas Capens49a88872013-06-20 09:54:03 -0400369
Jamie Madill1b452142013-07-12 14:51:11 -0400370 InsertBuiltInFunctions(shaderType, shaderSpec, resources, symbolTable);
Nicolas Capens49a88872013-06-20 09:54:03 -0400371
372 IdentifyBuiltIns(shaderType, shaderSpec, resources, symbolTable);
373
374 return true;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000375}
376
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400377void TCompiler::setResourceString()
378{
379 std::ostringstream strstream;
380 strstream << ":MaxVertexAttribs:" << compileResources.MaxVertexAttribs
381 << ":MaxVertexUniformVectors:" << compileResources.MaxVertexUniformVectors
382 << ":MaxVaryingVectors:" << compileResources.MaxVaryingVectors
383 << ":MaxVertexTextureImageUnits:" << compileResources.MaxVertexTextureImageUnits
384 << ":MaxCombinedTextureImageUnits:" << compileResources.MaxCombinedTextureImageUnits
385 << ":MaxTextureImageUnits:" << compileResources.MaxTextureImageUnits
386 << ":MaxFragmentUniformVectors:" << compileResources.MaxFragmentUniformVectors
387 << ":MaxDrawBuffers:" << compileResources.MaxDrawBuffers
388 << ":OES_standard_derivatives:" << compileResources.OES_standard_derivatives
389 << ":OES_EGL_image_external:" << compileResources.OES_EGL_image_external
390 << ":ARB_texture_rectangle:" << compileResources.ARB_texture_rectangle
391 << ":EXT_draw_buffers:" << compileResources.EXT_draw_buffers
392 << ":FragmentPrecisionHigh:" << compileResources.FragmentPrecisionHigh
393 << ":MaxExpressionComplexity:" << compileResources.MaxExpressionComplexity
394 << ":MaxCallStackDepth:" << compileResources.MaxCallStackDepth
395 << ":EXT_frag_depth:" << compileResources.EXT_frag_depth
396 << ":EXT_shader_texture_lod:" << compileResources.EXT_shader_texture_lod
Erik Dahlströmea7a2122014-11-17 16:15:57 +0100397 << ":EXT_shader_framebuffer_fetch:" << compileResources.EXT_shader_framebuffer_fetch
398 << ":NV_shader_framebuffer_fetch:" << compileResources.NV_shader_framebuffer_fetch
399 << ":ARM_shader_framebuffer_fetch:" << compileResources.ARM_shader_framebuffer_fetch
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400400 << ":MaxVertexOutputVectors:" << compileResources.MaxVertexOutputVectors
401 << ":MaxFragmentInputVectors:" << compileResources.MaxFragmentInputVectors
402 << ":MinProgramTexelOffset:" << compileResources.MinProgramTexelOffset
Olli Etuahoe61209a2014-09-26 12:01:17 +0300403 << ":MaxProgramTexelOffset:" << compileResources.MaxProgramTexelOffset
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200404 << ":NV_draw_buffers:" << compileResources.NV_draw_buffers
405 << ":WEBGL_debug_shader_precision:" << compileResources.WEBGL_debug_shader_precision;
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400406
407 builtInResourcesString = strstream.str();
408}
409
alokp@chromium.org07620a52010-09-23 17:53:56 +0000410void TCompiler::clearResults()
411{
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000412 arrayBoundsClamper.Cleanup();
alokp@chromium.org07620a52010-09-23 17:53:56 +0000413 infoSink.info.erase();
414 infoSink.obj.erase();
415 infoSink.debug.erase();
416
Jamie Madilled27c722014-07-02 15:31:23 -0400417 attributes.clear();
418 outputVariables.clear();
alokp@chromium.org07620a52010-09-23 17:53:56 +0000419 uniforms.clear();
Jamie Madill23a8a432014-07-09 13:27:42 -0400420 expandedUniforms.clear();
Zhenyao Mod2d340b2013-09-23 14:57:05 -0400421 varyings.clear();
Jamie Madilled27c722014-07-02 15:31:23 -0400422 interfaceBlocks.clear();
zmo@google.coma3b4ab42011-09-16 00:53:26 +0000423
424 builtInFunctionEmulator.Cleanup();
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +0000425
426 nameMap.clear();
alokp@chromium.org07620a52010-09-23 17:53:56 +0000427}
428
Austin Kinross3ae64652015-01-26 15:51:39 -0800429bool TCompiler::detectCallDepth(TIntermNode* inputRoot, TInfoSink& inputInfoSink, bool limitCallStackDepth)
zmo@google.comb1762df2011-07-30 02:04:23 +0000430{
Austin Kinross3ae64652015-01-26 15:51:39 -0800431 DetectCallDepth detect(inputInfoSink, limitCallStackDepth, maxCallStackDepth);
432 inputRoot->traverse(&detect);
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800433 switch (detect.detectCallDepth())
434 {
435 case DetectCallDepth::kErrorNone:
436 return true;
437 case DetectCallDepth::kErrorMissingMain:
Austin Kinross3ae64652015-01-26 15:51:39 -0800438 inputInfoSink.info.prefix(EPrefixError);
439 inputInfoSink.info << "Missing main()";
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800440 return false;
441 case DetectCallDepth::kErrorRecursion:
Austin Kinross3ae64652015-01-26 15:51:39 -0800442 inputInfoSink.info.prefix(EPrefixError);
443 inputInfoSink.info << "Function recursion detected";
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800444 return false;
445 case DetectCallDepth::kErrorMaxDepthExceeded:
Austin Kinross3ae64652015-01-26 15:51:39 -0800446 inputInfoSink.info.prefix(EPrefixError);
447 inputInfoSink.info << "Function call stack too deep";
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800448 return false;
449 default:
450 UNREACHABLE();
451 return false;
zmo@google.comb1762df2011-07-30 02:04:23 +0000452 }
453}
454
Jamie Madill05a80ce2013-06-20 11:55:49 -0400455bool TCompiler::validateOutputs(TIntermNode* root)
456{
457 ValidateOutputs validateOutputs(infoSink.info, compileResources.MaxDrawBuffers);
458 root->traverse(&validateOutputs);
459 return (validateOutputs.numErrors() == 0);
460}
461
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +0000462void TCompiler::rewriteCSSShader(TIntermNode* root)
463{
464 RenameFunction renamer("main(", "css_main(");
465 root->traverse(&renamer);
466}
467
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800468bool TCompiler::validateLimitations(TIntermNode* root)
469{
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000470 ValidateLimitations validate(shaderType, infoSink.info);
471 root->traverse(&validate);
472 return validate.numErrors() == 0;
473}
474
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000475bool TCompiler::enforceTimingRestrictions(TIntermNode* root, bool outputGraph)
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000476{
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800477 if (shaderSpec != SH_WEBGL_SPEC)
478 {
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000479 infoSink.info << "Timing restrictions must be enforced under the WebGL spec.";
480 return false;
481 }
482
Jamie Madill183bde52014-07-02 15:31:19 -0400483 if (shaderType == GL_FRAGMENT_SHADER)
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800484 {
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000485 TDependencyGraph graph(root);
486
487 // Output any errors first.
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000488 bool success = enforceFragmentShaderTimingRestrictions(graph);
Jamie Madill5508f392014-02-20 13:31:36 -0500489
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000490 // Then, output the dependency graph.
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800491 if (outputGraph)
492 {
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000493 TDependencyGraphOutput output(infoSink.info);
494 output.outputAllSpanningTrees(graph);
495 }
Jamie Madill5508f392014-02-20 13:31:36 -0500496
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000497 return success;
498 }
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800499 else
500 {
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000501 return enforceVertexShaderTimingRestrictions(root);
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000502 }
503}
504
Jamie Madilleb1a0102013-07-08 13:31:38 -0400505bool TCompiler::limitExpressionComplexity(TIntermNode* root)
506{
Jamie Madill6654bc92014-03-26 14:01:57 -0400507 TMaxDepthTraverser traverser(maxExpressionComplexity+1);
Jamie Madilleb1a0102013-07-08 13:31:38 -0400508 root->traverse(&traverser);
Jamie Madill6654bc92014-03-26 14:01:57 -0400509
510 if (traverser.getMaxDepth() > maxExpressionComplexity)
511 {
512 infoSink.info << "Expression too complex.";
513 return false;
514 }
515
Jamie Madilleb1a0102013-07-08 13:31:38 -0400516 TDependencyGraph graph(root);
517
518 for (TFunctionCallVector::const_iterator iter = graph.beginUserDefinedFunctionCalls();
519 iter != graph.endUserDefinedFunctionCalls();
520 ++iter)
521 {
522 TGraphFunctionCall* samplerSymbol = *iter;
523 TDependencyGraphTraverser graphTraverser;
524 samplerSymbol->traverse(&graphTraverser);
525 }
526
Jamie Madilleb1a0102013-07-08 13:31:38 -0400527 return true;
528}
529
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000530bool TCompiler::enforceFragmentShaderTimingRestrictions(const TDependencyGraph& graph)
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000531{
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000532 RestrictFragmentShaderTiming restrictor(infoSink.info);
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000533 restrictor.enforceRestrictions(graph);
534 return restrictor.numErrors() == 0;
535}
536
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000537bool TCompiler::enforceVertexShaderTimingRestrictions(TIntermNode* root)
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000538{
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000539 RestrictVertexShaderTiming restrictor(infoSink.info);
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000540 restrictor.enforceRestrictions(root);
541 return restrictor.numErrors() == 0;
542}
543
Zhenyao Mo74da9f22013-09-23 14:57:01 -0400544void TCompiler::collectVariables(TIntermNode* root)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000545{
Jamie Madilla2fbb842014-09-03 09:40:47 -0400546 sh::CollectVariables collect(&attributes,
547 &outputVariables,
548 &uniforms,
549 &varyings,
550 &interfaceBlocks,
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700551 hashFunction,
552 symbolTable);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000553 root->traverse(&collect);
Jamie Madill23a8a432014-07-09 13:27:42 -0400554
Zhenyao Mo409078f2014-10-28 13:23:18 -0700555 // This is for enforcePackingRestriction().
556 sh::ExpandUniforms(uniforms, &expandedUniforms);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000557}
zmo@google.comfd747b82011-04-23 01:30:07 +0000558
gman@chromium.org8d804792012-10-17 21:33:48 +0000559bool TCompiler::enforcePackingRestrictions()
560{
561 VariablePacker packer;
Jamie Madill23a8a432014-07-09 13:27:42 -0400562 return packer.CheckVariablesWithinPackingLimits(maxUniformVectors, expandedUniforms);
gman@chromium.org8d804792012-10-17 21:33:48 +0000563}
564
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800565void TCompiler::initializeGLPosition(TIntermNode* root)
566{
567 InitializeVariables::InitVariableInfoList variables;
568 InitializeVariables::InitVariableInfo var(
569 "gl_Position", TType(EbtFloat, EbpUndefined, EvqPosition, 4));
570 variables.push_back(var);
571 InitializeVariables initializer(variables);
572 root->traverse(&initializer);
573}
574
575void TCompiler::initializeVaryingsWithoutStaticUse(TIntermNode* root)
576{
577 InitializeVariables::InitVariableInfoList variables;
578 for (size_t ii = 0; ii < varyings.size(); ++ii)
579 {
Jamie Madilla718c1e2014-07-02 15:31:22 -0400580 const sh::Varying& varying = varyings[ii];
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800581 if (varying.staticUse)
582 continue;
Jamie Madillaa72d782014-07-02 15:31:19 -0400583 unsigned char primarySize = static_cast<unsigned char>(gl::VariableColumnCount(varying.type));
584 unsigned char secondarySize = static_cast<unsigned char>(gl::VariableRowCount(varying.type));
Jamie Madilla718c1e2014-07-02 15:31:22 -0400585 TType type(EbtFloat, EbpUndefined, EvqVaryingOut, primarySize, secondarySize, varying.isArray());
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800586 TString name = varying.name.c_str();
Jamie Madilla718c1e2014-07-02 15:31:22 -0400587 if (varying.isArray())
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800588 {
Jamie Madilla718c1e2014-07-02 15:31:22 -0400589 type.setArraySize(varying.arraySize);
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800590 name = name.substr(0, name.find_first_of('['));
591 }
592
593 InitializeVariables::InitVariableInfo var(name, type);
594 variables.push_back(var);
595 }
596 InitializeVariables initializer(variables);
597 root->traverse(&initializer);
598}
599
zmo@google.com5601ea02011-06-10 18:23:25 +0000600const TExtensionBehavior& TCompiler::getExtensionBehavior() const
601{
602 return extensionBehavior;
603}
zmo@google.com32e97312011-08-24 01:03:11 +0000604
shannon.woods%transgaming.com@gtempaccount.com18b4c4b2013-04-13 03:31:40 +0000605const ShBuiltInResources& TCompiler::getResources() const
606{
607 return compileResources;
608}
609
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000610const ArrayBoundsClamper& TCompiler::getArrayBoundsClamper() const
611{
612 return arrayBoundsClamper;
613}
614
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000615ShArrayIndexClampingStrategy TCompiler::getArrayIndexClampingStrategy() const
616{
617 return clampingStrategy;
618}
619
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +0200620const BuiltInFunctionEmulatorGLSL& TCompiler::getBuiltInFunctionEmulator() const
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000621{
622 return builtInFunctionEmulator;
623}
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700624
625void TCompiler::writePragma()
626{
627 TInfoSinkBase &sink = infoSink.obj;
628 if (mPragma.stdgl.invariantAll)
629 sink << "#pragma STDGL invariant(all)\n";
630}