blob: 8c5e50fedb5ceb4b2d1e1178149df9a46b89e3ae [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"
Corentin Wallez71d147f2015-02-11 11:15:24 -08008#include "compiler/translator/CallDAG.h"
Geoff Lang17732822013-08-29 13:46:49 -04009#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),
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200128 builtInFunctionEmulator(),
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200129 mSourcePath(NULL)
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
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200162TIntermNode *TCompiler::compileTreeForTesting(const char* const shaderStrings[],
163 size_t numStrings, int compileOptions)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000164{
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200165 return compileTreeImpl(shaderStrings, numStrings, compileOptions);
166}
167
168TIntermNode *TCompiler::compileTreeImpl(const char* const shaderStrings[],
169 size_t numStrings, int compileOptions)
170{
alokp@chromium.org07620a52010-09-23 17:53:56 +0000171 clearResults();
172
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200173 ASSERT(numStrings > 0);
174 ASSERT(GetGlobalPoolAllocator());
alokp@chromium.org07620a52010-09-23 17:53:56 +0000175
David Yen0fbd1282015-02-02 14:46:09 -0800176 // Reset the extension behavior for each compilation unit.
177 ResetExtensionBehavior(extensionBehavior);
178
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000179 // If compiling for WebGL, validate loop and indexing as well.
Jamie Madill5508f392014-02-20 13:31:36 -0500180 if (IsWebGLBasedSpec(shaderSpec))
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000181 compileOptions |= SH_VALIDATE_LOOP_INDEXING;
alokp@chromium.org1f299542010-11-12 15:50:23 +0000182
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000183 // First string is path of source file if flag is set. The actual source follows.
shannon.woods@transgaming.comd64b3da2013-02-28 23:19:26 +0000184 size_t firstSource = 0;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000185 if (compileOptions & SH_SOURCE_PATH)
186 {
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200187 mSourcePath = shaderStrings[0];
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000188 ++firstSource;
189 }
190
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200191 bool debugShaderPrecision = getResources().WEBGL_debug_shader_precision == 1;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000192 TIntermediate intermediate(infoSink);
193 TParseContext parseContext(symbolTable, extensionBehavior, intermediate,
zmo@google.comdc4b4f82011-06-17 00:42:53 +0000194 shaderType, shaderSpec, compileOptions, true,
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200195 infoSink, debugShaderPrecision);
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200196
shannon.woods%transgaming.com@gtempaccount.comcbb6b6a2013-04-13 03:27:47 +0000197 parseContext.fragmentPrecisionHigh = fragmentPrecisionHigh;
Alok Priyadarshi8156b6b2013-09-23 14:56:58 -0400198 SetGlobalParseContext(&parseContext);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000199
200 // We preserve symbols at the built-in level from compile-to-compile.
201 // Start pushing the user-defined symbols at global level.
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400202 TScopedSymbolTableLevel scopedSymbolLevel(&symbolTable);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000203
204 // Parse shader.
205 bool success =
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000206 (PaParseStrings(numStrings - firstSource, &shaderStrings[firstSource], NULL, &parseContext) == 0) &&
alokp@chromium.org07620a52010-09-23 17:53:56 +0000207 (parseContext.treeRoot != NULL);
shannon.woods%transgaming.com@gtempaccount.com0bbed382013-04-13 03:38:07 +0000208
shannon.woods%transgaming.com@gtempaccount.com5524db02013-04-13 03:38:16 +0000209 shaderVersion = parseContext.getShaderVersion();
Zhenyao Modb9b40b2014-10-29 15:00:04 -0700210 if (success && MapSpecToShaderVersion(shaderSpec) < shaderVersion)
211 {
212 infoSink.info.prefix(EPrefixError);
213 infoSink.info << "unsupported shader version";
214 success = false;
215 }
shannon.woods%transgaming.com@gtempaccount.com0bbed382013-04-13 03:38:07 +0000216
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200217 TIntermNode *root = NULL;
218
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800219 if (success)
220 {
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700221 mPragma = parseContext.pragma();
222 if (mPragma.stdgl.invariantAll)
223 {
224 symbolTable.setGlobalInvariant();
225 }
226
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200227 root = parseContext.treeRoot;
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000228 success = intermediate.postProcess(root);
229
Jamie Madill6654bc92014-03-26 14:01:57 -0400230 // Disallow expressions deemed too complex.
231 if (success && (compileOptions & SH_LIMIT_EXPRESSION_COMPLEXITY))
232 success = limitExpressionComplexity(root);
233
Corentin Wallez71d147f2015-02-11 11:15:24 -0800234 // Create the function DAG and check there is no recursion
zmo@google.comb1762df2011-07-30 02:04:23 +0000235 if (success)
Corentin Wallez71d147f2015-02-11 11:15:24 -0800236 success = initCallDag(root);
237
238 if (success && (compileOptions & SH_LIMIT_CALL_STACK_DEPTH))
239 success = checkCallDepth();
240
241 // Checks which functions are used and if "main" exists
242 if (success)
243 {
244 functionMetadata.clear();
245 functionMetadata.resize(mCallDag.size());
246 success = tagUsedFunctions();
247 }
zmo@google.comb1762df2011-07-30 02:04:23 +0000248
Jamie Madill183bde52014-07-02 15:31:19 -0400249 if (success && shaderVersion == 300 && shaderType == GL_FRAGMENT_SHADER)
Jamie Madill05a80ce2013-06-20 11:55:49 -0400250 success = validateOutputs(root);
251
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000252 if (success && (compileOptions & SH_VALIDATE_LOOP_INDEXING))
253 success = validateLimitations(root);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000254
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000255 if (success && (compileOptions & SH_TIMING_RESTRICTIONS))
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000256 success = enforceTimingRestrictions(root, (compileOptions & SH_DEPENDENCY_GRAPH) != 0);
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000257
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +0000258 if (success && shaderSpec == SH_CSS_SHADERS_SPEC)
259 rewriteCSSShader(root);
260
zmo@google.com0c6bb7a2011-08-17 19:39:58 +0000261 // Unroll for-loop markup needs to happen after validateLimitations pass.
262 if (success && (compileOptions & SH_UNROLL_FOR_LOOP_WITH_INTEGER_INDEX))
Zhenyao Mo3cdfcce2014-03-07 13:00:08 -0800263 {
264 ForLoopUnrollMarker marker(ForLoopUnrollMarker::kIntegerIndex);
Zhenyao Mo550c6002014-02-26 15:40:48 -0800265 root->traverse(&marker);
266 }
267 if (success && (compileOptions & SH_UNROLL_FOR_LOOP_WITH_SAMPLER_ARRAY_INDEX))
Zhenyao Mo3cdfcce2014-03-07 13:00:08 -0800268 {
269 ForLoopUnrollMarker marker(ForLoopUnrollMarker::kSamplerArrayIndex);
Zhenyao Mo550c6002014-02-26 15:40:48 -0800270 root->traverse(&marker);
271 if (marker.samplerArrayIndexIsFloatLoopIndex())
272 {
273 infoSink.info.prefix(EPrefixError);
274 infoSink.info << "sampler array index is float loop index";
275 success = false;
276 }
277 }
zmo@google.com0c6bb7a2011-08-17 19:39:58 +0000278
zmo@google.com32e97312011-08-24 01:03:11 +0000279 // Built-in function emulation needs to happen after validateLimitations pass.
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200280 if (success)
281 {
282 initBuiltInFunctionEmulator(&builtInFunctionEmulator, compileOptions);
zmo@google.com32e97312011-08-24 01:03:11 +0000283 builtInFunctionEmulator.MarkBuiltInFunctionsForEmulation(root);
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200284 }
zmo@google.com32e97312011-08-24 01:03:11 +0000285
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000286 // Clamping uniform array bounds needs to happen after validateLimitations pass.
287 if (success && (compileOptions & SH_CLAMP_INDIRECT_ARRAY_BOUNDS))
288 arrayBoundsClamper.MarkIndirectArrayBoundsForClamping(root);
289
Jamie Madill183bde52014-07-02 15:31:19 -0400290 if (success && shaderType == GL_VERTEX_SHADER && (compileOptions & SH_INIT_GL_POSITION))
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800291 initializeGLPosition(root);
Zhenyao Moac44cd22013-09-23 14:57:09 -0400292
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800293 if (success && (compileOptions & SH_UNFOLD_SHORT_CIRCUIT))
294 {
Zhenyao Mo7cab38b2013-10-15 12:59:30 -0700295 UnfoldShortCircuitAST unfoldShortCircuit;
296 root->traverse(&unfoldShortCircuit);
297 unfoldShortCircuit.updateTree();
298 }
299
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800300 if (success && (compileOptions & SH_VARIABLES))
301 {
Zhenyao Mo74da9f22013-09-23 14:57:01 -0400302 collectVariables(root);
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800303 if (compileOptions & SH_ENFORCE_PACKING_RESTRICTIONS)
304 {
gman@chromium.org8d804792012-10-17 21:33:48 +0000305 success = enforcePackingRestrictions();
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800306 if (!success)
307 {
Jamie Madill075edd82013-07-08 13:30:19 -0400308 infoSink.info.prefix(EPrefixError);
309 infoSink.info << "too many uniforms";
gman@chromium.org8d804792012-10-17 21:33:48 +0000310 }
311 }
Jamie Madill183bde52014-07-02 15:31:19 -0400312 if (success && shaderType == GL_VERTEX_SHADER &&
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800313 (compileOptions & SH_INIT_VARYINGS_WITHOUT_STATIC_USE))
314 initializeVaryingsWithoutStaticUse(root);
gman@chromium.org8d804792012-10-17 21:33:48 +0000315 }
zmo@google.comfd747b82011-04-23 01:30:07 +0000316
Zhenyao Mocd68fe72014-07-11 10:45:44 -0700317 if (success && (compileOptions & SH_SCALARIZE_VEC_AND_MAT_CONSTRUCTOR_ARGS))
318 {
Zhenyao Modaf56572014-08-06 16:18:30 -0700319 ScalarizeVecAndMatConstructorArgs scalarizer(
320 shaderType, fragmentPrecisionHigh);
Zhenyao Mocd68fe72014-07-11 10:45:44 -0700321 root->traverse(&scalarizer);
322 }
323
Zhenyao Moe740add2014-07-18 17:01:01 -0700324 if (success && (compileOptions & SH_REGENERATE_STRUCT_NAMES))
325 {
326 RegenerateStructNames gen(symbolTable, shaderVersion);
327 root->traverse(&gen);
328 }
alokp@chromium.org07620a52010-09-23 17:53:56 +0000329 }
330
Zhenyao Mo7faf1a12014-04-25 18:03:56 -0700331 SetGlobalParseContext(NULL);
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200332 if (success)
333 return root;
334
335 return NULL;
336}
337
338bool TCompiler::compile(const char* const shaderStrings[],
339 size_t numStrings, int compileOptions)
340{
341 if (numStrings == 0)
342 return true;
343
344 TScopedPoolAllocator scopedAlloc(&allocator);
345 TIntermNode *root = compileTreeImpl(shaderStrings, numStrings, compileOptions);
346
347 if (root)
348 {
349 if (compileOptions & SH_INTERMEDIATE_TREE)
350 TIntermediate::outputTree(root, infoSink.info);
351
352 if (compileOptions & SH_OBJECT_CODE)
353 translate(root, compileOptions);
354
355 // The IntermNode tree doesn't need to be deleted here, since the
356 // memory will be freed in a big chunk by the PoolAllocator.
357 return true;
358 }
359 return false;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000360}
361
Nicolas Capens49a88872013-06-20 09:54:03 -0400362bool TCompiler::InitBuiltInSymbolTable(const ShBuiltInResources &resources)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000363{
shannon.woods%transgaming.com@gtempaccount.com18b4c4b2013-04-13 03:31:40 +0000364 compileResources = resources;
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400365 setResourceString();
shannonwoods@chromium.org2ac0be92013-05-30 00:02:27 +0000366
Nicolas Capens49a88872013-06-20 09:54:03 -0400367 assert(symbolTable.isEmpty());
368 symbolTable.push(); // COMMON_BUILTINS
369 symbolTable.push(); // ESSL1_BUILTINS
370 symbolTable.push(); // ESSL3_BUILTINS
shannonwoods@chromium.org2ac0be92013-05-30 00:02:27 +0000371
Nicolas Capens49a88872013-06-20 09:54:03 -0400372 TPublicType integer;
373 integer.type = EbtInt;
374 integer.primarySize = 1;
375 integer.secondarySize = 1;
376 integer.array = false;
377
378 TPublicType floatingPoint;
379 floatingPoint.type = EbtFloat;
380 floatingPoint.primarySize = 1;
381 floatingPoint.secondarySize = 1;
382 floatingPoint.array = false;
383
Zhenyao Moa5a1dfc2013-09-23 14:57:03 -0400384 TPublicType sampler;
385 sampler.primarySize = 1;
386 sampler.secondarySize = 1;
387 sampler.array = false;
388
Nicolas Capens49a88872013-06-20 09:54:03 -0400389 switch(shaderType)
390 {
Jamie Madill183bde52014-07-02 15:31:19 -0400391 case GL_FRAGMENT_SHADER:
Nicolas Capens49a88872013-06-20 09:54:03 -0400392 symbolTable.setDefaultPrecision(integer, EbpMedium);
393 break;
Jamie Madill183bde52014-07-02 15:31:19 -0400394 case GL_VERTEX_SHADER:
Nicolas Capens49a88872013-06-20 09:54:03 -0400395 symbolTable.setDefaultPrecision(integer, EbpHigh);
396 symbolTable.setDefaultPrecision(floatingPoint, EbpHigh);
397 break;
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800398 default:
399 assert(false && "Language not supported");
Nicolas Capens49a88872013-06-20 09:54:03 -0400400 }
Zhenyao Moa5a1dfc2013-09-23 14:57:03 -0400401 // We set defaults for all the sampler types, even those that are
402 // only available if an extension exists.
403 for (int samplerType = EbtGuardSamplerBegin + 1;
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800404 samplerType < EbtGuardSamplerEnd; ++samplerType)
405 {
Zhenyao Moa5a1dfc2013-09-23 14:57:03 -0400406 sampler.type = static_cast<TBasicType>(samplerType);
407 symbolTable.setDefaultPrecision(sampler, EbpLow);
408 }
Nicolas Capens49a88872013-06-20 09:54:03 -0400409
Jamie Madill1b452142013-07-12 14:51:11 -0400410 InsertBuiltInFunctions(shaderType, shaderSpec, resources, symbolTable);
Nicolas Capens49a88872013-06-20 09:54:03 -0400411
412 IdentifyBuiltIns(shaderType, shaderSpec, resources, symbolTable);
413
414 return true;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000415}
416
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400417void TCompiler::setResourceString()
418{
419 std::ostringstream strstream;
420 strstream << ":MaxVertexAttribs:" << compileResources.MaxVertexAttribs
421 << ":MaxVertexUniformVectors:" << compileResources.MaxVertexUniformVectors
422 << ":MaxVaryingVectors:" << compileResources.MaxVaryingVectors
423 << ":MaxVertexTextureImageUnits:" << compileResources.MaxVertexTextureImageUnits
424 << ":MaxCombinedTextureImageUnits:" << compileResources.MaxCombinedTextureImageUnits
425 << ":MaxTextureImageUnits:" << compileResources.MaxTextureImageUnits
426 << ":MaxFragmentUniformVectors:" << compileResources.MaxFragmentUniformVectors
427 << ":MaxDrawBuffers:" << compileResources.MaxDrawBuffers
428 << ":OES_standard_derivatives:" << compileResources.OES_standard_derivatives
429 << ":OES_EGL_image_external:" << compileResources.OES_EGL_image_external
430 << ":ARB_texture_rectangle:" << compileResources.ARB_texture_rectangle
431 << ":EXT_draw_buffers:" << compileResources.EXT_draw_buffers
432 << ":FragmentPrecisionHigh:" << compileResources.FragmentPrecisionHigh
433 << ":MaxExpressionComplexity:" << compileResources.MaxExpressionComplexity
434 << ":MaxCallStackDepth:" << compileResources.MaxCallStackDepth
435 << ":EXT_frag_depth:" << compileResources.EXT_frag_depth
436 << ":EXT_shader_texture_lod:" << compileResources.EXT_shader_texture_lod
Erik Dahlströmea7a2122014-11-17 16:15:57 +0100437 << ":EXT_shader_framebuffer_fetch:" << compileResources.EXT_shader_framebuffer_fetch
438 << ":NV_shader_framebuffer_fetch:" << compileResources.NV_shader_framebuffer_fetch
439 << ":ARM_shader_framebuffer_fetch:" << compileResources.ARM_shader_framebuffer_fetch
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400440 << ":MaxVertexOutputVectors:" << compileResources.MaxVertexOutputVectors
441 << ":MaxFragmentInputVectors:" << compileResources.MaxFragmentInputVectors
442 << ":MinProgramTexelOffset:" << compileResources.MinProgramTexelOffset
Olli Etuahoe61209a2014-09-26 12:01:17 +0300443 << ":MaxProgramTexelOffset:" << compileResources.MaxProgramTexelOffset
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200444 << ":NV_draw_buffers:" << compileResources.NV_draw_buffers
445 << ":WEBGL_debug_shader_precision:" << compileResources.WEBGL_debug_shader_precision;
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400446
447 builtInResourcesString = strstream.str();
448}
449
alokp@chromium.org07620a52010-09-23 17:53:56 +0000450void TCompiler::clearResults()
451{
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000452 arrayBoundsClamper.Cleanup();
alokp@chromium.org07620a52010-09-23 17:53:56 +0000453 infoSink.info.erase();
454 infoSink.obj.erase();
455 infoSink.debug.erase();
456
Jamie Madilled27c722014-07-02 15:31:23 -0400457 attributes.clear();
458 outputVariables.clear();
alokp@chromium.org07620a52010-09-23 17:53:56 +0000459 uniforms.clear();
Jamie Madill23a8a432014-07-09 13:27:42 -0400460 expandedUniforms.clear();
Zhenyao Mod2d340b2013-09-23 14:57:05 -0400461 varyings.clear();
Jamie Madilled27c722014-07-02 15:31:23 -0400462 interfaceBlocks.clear();
zmo@google.coma3b4ab42011-09-16 00:53:26 +0000463
464 builtInFunctionEmulator.Cleanup();
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +0000465
466 nameMap.clear();
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200467
468 mSourcePath = NULL;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000469}
470
Corentin Wallez71d147f2015-02-11 11:15:24 -0800471bool TCompiler::initCallDag(TIntermNode *root)
zmo@google.comb1762df2011-07-30 02:04:23 +0000472{
Corentin Wallez71d147f2015-02-11 11:15:24 -0800473 mCallDag.clear();
474
475 switch (mCallDag.init(root, &infoSink.info))
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800476 {
Corentin Wallez71d147f2015-02-11 11:15:24 -0800477 case CallDAG::INITDAG_SUCCESS:
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800478 return true;
Corentin Wallez71d147f2015-02-11 11:15:24 -0800479 case CallDAG::INITDAG_RECURSION:
480 infoSink.info.prefix(EPrefixError);
481 infoSink.info << "Function recursion detected";
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800482 return false;
Corentin Wallez71d147f2015-02-11 11:15:24 -0800483 case CallDAG::INITDAG_UNDEFINED:
484 infoSink.info.prefix(EPrefixError);
485 infoSink.info << "Unimplemented function detected";
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800486 return false;
Corentin Wallez71d147f2015-02-11 11:15:24 -0800487 }
488
489 UNREACHABLE();
490 return true;
491}
492
493bool TCompiler::checkCallDepth()
494{
495 std::vector<int> depths(mCallDag.size());
496
497 for (size_t i = 0; i < mCallDag.size(); i++)
498 {
499 int depth = 0;
500 auto &record = mCallDag.getRecordFromIndex(i);
501
502 for (auto &calleeIndex : record.callees)
503 {
504 depth = std::max(depth, depths[calleeIndex] + 1);
505 }
506
507 depths[i] = depth;
508
509 if (depth >= maxCallStackDepth)
510 {
511 // Trace back the function chain to have a meaningful info log.
512 infoSink.info.prefix(EPrefixError);
513 infoSink.info << "Call stack too deep (larger than " << maxCallStackDepth
514 << ") with the following call chain: " << record.name;
515
516 int currentFunction = i;
517 int currentDepth = depth;
518
519 while (currentFunction != -1)
520 {
521 infoSink.info << " -> " << mCallDag.getRecordFromIndex(currentFunction).name;
522
523 int nextFunction = -1;
524 for (auto& calleeIndex : mCallDag.getRecordFromIndex(currentFunction).callees)
525 {
526 if (depths[calleeIndex] == currentDepth - 1)
527 {
528 currentDepth--;
529 nextFunction = calleeIndex;
530 }
531 }
532
533 currentFunction = nextFunction;
534 }
535
536 return false;
537 }
538 }
539
540 return true;
541}
542
543bool TCompiler::tagUsedFunctions()
544{
545 // Search from main, starting from the end of the DAG as it usually is the root.
546 for (int i = mCallDag.size(); i-- > 0;)
547 {
548 if (mCallDag.getRecordFromIndex(i).name == "main(")
549 {
550 internalTagUsedFunction(i);
551 return true;
552 }
553 }
554
555 infoSink.info.prefix(EPrefixError);
556 infoSink.info << "Missing main()";
557 return false;
558}
559
560void TCompiler::internalTagUsedFunction(size_t index)
561{
562 if (functionMetadata[index].used)
563 {
564 return;
565 }
566
567 functionMetadata[index].used = true;
568
569 for (int calleeIndex : mCallDag.getRecordFromIndex(index).callees)
570 {
571 internalTagUsedFunction(calleeIndex);
zmo@google.comb1762df2011-07-30 02:04:23 +0000572 }
573}
574
Jamie Madill05a80ce2013-06-20 11:55:49 -0400575bool TCompiler::validateOutputs(TIntermNode* root)
576{
577 ValidateOutputs validateOutputs(infoSink.info, compileResources.MaxDrawBuffers);
578 root->traverse(&validateOutputs);
579 return (validateOutputs.numErrors() == 0);
580}
581
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +0000582void TCompiler::rewriteCSSShader(TIntermNode* root)
583{
584 RenameFunction renamer("main(", "css_main(");
585 root->traverse(&renamer);
586}
587
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800588bool TCompiler::validateLimitations(TIntermNode* root)
589{
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000590 ValidateLimitations validate(shaderType, infoSink.info);
591 root->traverse(&validate);
592 return validate.numErrors() == 0;
593}
594
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000595bool TCompiler::enforceTimingRestrictions(TIntermNode* root, bool outputGraph)
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000596{
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800597 if (shaderSpec != SH_WEBGL_SPEC)
598 {
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000599 infoSink.info << "Timing restrictions must be enforced under the WebGL spec.";
600 return false;
601 }
602
Jamie Madill183bde52014-07-02 15:31:19 -0400603 if (shaderType == GL_FRAGMENT_SHADER)
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800604 {
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000605 TDependencyGraph graph(root);
606
607 // Output any errors first.
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000608 bool success = enforceFragmentShaderTimingRestrictions(graph);
Jamie Madill5508f392014-02-20 13:31:36 -0500609
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000610 // Then, output the dependency graph.
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800611 if (outputGraph)
612 {
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000613 TDependencyGraphOutput output(infoSink.info);
614 output.outputAllSpanningTrees(graph);
615 }
Jamie Madill5508f392014-02-20 13:31:36 -0500616
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000617 return success;
618 }
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800619 else
620 {
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000621 return enforceVertexShaderTimingRestrictions(root);
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000622 }
623}
624
Jamie Madilleb1a0102013-07-08 13:31:38 -0400625bool TCompiler::limitExpressionComplexity(TIntermNode* root)
626{
Jamie Madill6654bc92014-03-26 14:01:57 -0400627 TMaxDepthTraverser traverser(maxExpressionComplexity+1);
Jamie Madilleb1a0102013-07-08 13:31:38 -0400628 root->traverse(&traverser);
Jamie Madill6654bc92014-03-26 14:01:57 -0400629
630 if (traverser.getMaxDepth() > maxExpressionComplexity)
631 {
632 infoSink.info << "Expression too complex.";
633 return false;
634 }
635
Jamie Madilleb1a0102013-07-08 13:31:38 -0400636 TDependencyGraph graph(root);
637
638 for (TFunctionCallVector::const_iterator iter = graph.beginUserDefinedFunctionCalls();
639 iter != graph.endUserDefinedFunctionCalls();
640 ++iter)
641 {
642 TGraphFunctionCall* samplerSymbol = *iter;
643 TDependencyGraphTraverser graphTraverser;
644 samplerSymbol->traverse(&graphTraverser);
645 }
646
Jamie Madilleb1a0102013-07-08 13:31:38 -0400647 return true;
648}
649
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000650bool TCompiler::enforceFragmentShaderTimingRestrictions(const TDependencyGraph& graph)
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000651{
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000652 RestrictFragmentShaderTiming restrictor(infoSink.info);
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000653 restrictor.enforceRestrictions(graph);
654 return restrictor.numErrors() == 0;
655}
656
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000657bool TCompiler::enforceVertexShaderTimingRestrictions(TIntermNode* root)
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000658{
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000659 RestrictVertexShaderTiming restrictor(infoSink.info);
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000660 restrictor.enforceRestrictions(root);
661 return restrictor.numErrors() == 0;
662}
663
Zhenyao Mo74da9f22013-09-23 14:57:01 -0400664void TCompiler::collectVariables(TIntermNode* root)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000665{
Jamie Madilla2fbb842014-09-03 09:40:47 -0400666 sh::CollectVariables collect(&attributes,
667 &outputVariables,
668 &uniforms,
669 &varyings,
670 &interfaceBlocks,
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700671 hashFunction,
672 symbolTable);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000673 root->traverse(&collect);
Jamie Madill23a8a432014-07-09 13:27:42 -0400674
Zhenyao Mo409078f2014-10-28 13:23:18 -0700675 // This is for enforcePackingRestriction().
676 sh::ExpandUniforms(uniforms, &expandedUniforms);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000677}
zmo@google.comfd747b82011-04-23 01:30:07 +0000678
gman@chromium.org8d804792012-10-17 21:33:48 +0000679bool TCompiler::enforcePackingRestrictions()
680{
681 VariablePacker packer;
Jamie Madill23a8a432014-07-09 13:27:42 -0400682 return packer.CheckVariablesWithinPackingLimits(maxUniformVectors, expandedUniforms);
gman@chromium.org8d804792012-10-17 21:33:48 +0000683}
684
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800685void TCompiler::initializeGLPosition(TIntermNode* root)
686{
687 InitializeVariables::InitVariableInfoList variables;
688 InitializeVariables::InitVariableInfo var(
689 "gl_Position", TType(EbtFloat, EbpUndefined, EvqPosition, 4));
690 variables.push_back(var);
691 InitializeVariables initializer(variables);
692 root->traverse(&initializer);
693}
694
695void TCompiler::initializeVaryingsWithoutStaticUse(TIntermNode* root)
696{
697 InitializeVariables::InitVariableInfoList variables;
698 for (size_t ii = 0; ii < varyings.size(); ++ii)
699 {
Jamie Madilla718c1e2014-07-02 15:31:22 -0400700 const sh::Varying& varying = varyings[ii];
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800701 if (varying.staticUse)
702 continue;
Jamie Madillaa72d782014-07-02 15:31:19 -0400703 unsigned char primarySize = static_cast<unsigned char>(gl::VariableColumnCount(varying.type));
704 unsigned char secondarySize = static_cast<unsigned char>(gl::VariableRowCount(varying.type));
Jamie Madilla718c1e2014-07-02 15:31:22 -0400705 TType type(EbtFloat, EbpUndefined, EvqVaryingOut, primarySize, secondarySize, varying.isArray());
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800706 TString name = varying.name.c_str();
Jamie Madilla718c1e2014-07-02 15:31:22 -0400707 if (varying.isArray())
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800708 {
Jamie Madilla718c1e2014-07-02 15:31:22 -0400709 type.setArraySize(varying.arraySize);
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800710 name = name.substr(0, name.find_first_of('['));
711 }
712
713 InitializeVariables::InitVariableInfo var(name, type);
714 variables.push_back(var);
715 }
716 InitializeVariables initializer(variables);
717 root->traverse(&initializer);
718}
719
zmo@google.com5601ea02011-06-10 18:23:25 +0000720const TExtensionBehavior& TCompiler::getExtensionBehavior() const
721{
722 return extensionBehavior;
723}
zmo@google.com32e97312011-08-24 01:03:11 +0000724
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200725const char *TCompiler::getSourcePath() const
726{
727 return mSourcePath;
728}
729
shannon.woods%transgaming.com@gtempaccount.com18b4c4b2013-04-13 03:31:40 +0000730const ShBuiltInResources& TCompiler::getResources() const
731{
732 return compileResources;
733}
734
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000735const ArrayBoundsClamper& TCompiler::getArrayBoundsClamper() const
736{
737 return arrayBoundsClamper;
738}
739
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000740ShArrayIndexClampingStrategy TCompiler::getArrayIndexClampingStrategy() const
741{
742 return clampingStrategy;
743}
744
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200745const BuiltInFunctionEmulator& TCompiler::getBuiltInFunctionEmulator() const
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000746{
747 return builtInFunctionEmulator;
748}
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700749
750void TCompiler::writePragma()
751{
752 TInfoSinkBase &sink = infoSink.obj;
753 if (mPragma.stdgl.invariantAll)
754 sink << "#pragma STDGL invariant(all)\n";
755}