blob: 58dd39faab5164de12bdd23e066696d2a1c11492 [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
Corentin Wallez8b28a8b2016-09-15 19:47:56 -04007#include "compiler/translator/Compiler.h"
8
9#include <sstream>
10
11#include "angle_gl.h"
12#include "common/utilities.h"
Qiankun Miao09cfac62016-09-06 17:25:16 +080013#include "compiler/translator/AddAndTrueToLoopCondition.h"
Dmitry Skiba01971112015-07-10 14:54:00 -040014#include "compiler/translator/Cache.h"
Corentin Wallez71d147f2015-02-11 11:15:24 -080015#include "compiler/translator/CallDAG.h"
Olli Etuaho3d932d82016-04-12 11:10:30 +030016#include "compiler/translator/DeferGlobalInitializers.h"
Zhenyao Mo4e94fea2016-08-09 14:31:37 -070017#include "compiler/translator/EmulateGLFragColorBroadcast.h"
Jamie Madilld5696192016-10-06 11:09:24 -040018#include "compiler/translator/EmulatePrecision.h"
Geoff Lang17732822013-08-29 13:46:49 -040019#include "compiler/translator/Initialize.h"
Geoff Lang17732822013-08-29 13:46:49 -040020#include "compiler/translator/InitializeParseContext.h"
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080021#include "compiler/translator/InitializeVariables.h"
Jamie Madill6b9cb252013-10-17 10:45:47 -040022#include "compiler/translator/ParseContext.h"
Olli Etuahoc6833112015-04-22 15:15:54 +030023#include "compiler/translator/PruneEmptyDeclarations.h"
Zhenyao Moe740add2014-07-18 17:01:01 -070024#include "compiler/translator/RegenerateStructNames.h"
Qiankun Miao705a9192016-08-29 10:05:27 +080025#include "compiler/translator/RemoveInvariantDeclaration.h"
Olli Etuaho5c407bb2015-06-01 12:20:39 +030026#include "compiler/translator/RemovePow.h"
Corentin Wallezd4b50542015-09-28 12:19:26 -070027#include "compiler/translator/RewriteDoWhile.h"
Zhenyao Mocd68fe72014-07-11 10:45:44 -070028#include "compiler/translator/ScalarizeVecAndMatConstructorArgs.h"
Zhenyao Mo7cab38b2013-10-15 12:59:30 -070029#include "compiler/translator/UnfoldShortCircuitAST.h"
Qin Jiajia7835b522016-10-08 11:20:17 +080030#include "compiler/translator/UseInterfaceBlockFields.h"
Geoff Lang17732822013-08-29 13:46:49 -040031#include "compiler/translator/ValidateLimitations.h"
Olli Etuaho19d1dc92016-03-08 17:18:46 +020032#include "compiler/translator/ValidateMaxParameters.h"
Olli Etuaho09b04a22016-12-15 13:30:26 +000033#include "compiler/translator/ValidateMultiviewWebGL.h"
Geoff Lang17732822013-08-29 13:46:49 -040034#include "compiler/translator/ValidateOutputs.h"
35#include "compiler/translator/VariablePacker.h"
shannon.woods@transgaming.comda1ed362013-01-25 21:54:57 +000036#include "third_party/compiler/ArrayBoundsClamper.h"
Corentin Wallez28b65282016-06-16 07:24:50 -070037
Jamie Madillacb4b812016-11-07 13:50:29 -050038namespace sh
39{
40
Corentin Wallez28b65282016-06-16 07:24:50 -070041namespace
42{
43
44#if defined(ANGLE_ENABLE_FUZZER_CORPUS_OUTPUT)
45void DumpFuzzerCase(char const *const *shaderStrings,
46 size_t numStrings,
47 uint32_t type,
48 uint32_t spec,
49 uint32_t output,
50 uint64_t options)
51{
52 static int fileIndex = 0;
53
54 std::ostringstream o;
55 o << "corpus/" << fileIndex++ << ".sample";
56 std::string s = o.str();
57
58 // Must match the input format of the fuzzer
59 FILE *f = fopen(s.c_str(), "w");
60 fwrite(&type, sizeof(type), 1, f);
61 fwrite(&spec, sizeof(spec), 1, f);
62 fwrite(&output, sizeof(output), 1, f);
63 fwrite(&options, sizeof(options), 1, f);
64
65 char zero[128 - 20] = {0};
66 fwrite(&zero, 128 - 20, 1, f);
67
68 for (size_t i = 0; i < numStrings; i++)
69 {
70 fwrite(shaderStrings[i], sizeof(char), strlen(shaderStrings[i]), f);
71 }
72 fwrite(&zero, 1, 1, f);
73
74 fclose(f);
75}
76#endif // defined(ANGLE_ENABLE_FUZZER_CORPUS_OUTPUT)
77} // anonymous namespace
78
Jamie Madill5508f392014-02-20 13:31:36 -050079bool IsWebGLBasedSpec(ShShaderSpec spec)
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +000080{
Qiankun Miaoc2c5fc42016-08-31 15:24:22 +080081 return (spec == SH_WEBGL_SPEC || spec == SH_WEBGL2_SPEC || spec == SH_WEBGL3_SPEC);
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +000082}
83
Qingqing Dengad0d0792015-04-08 14:25:06 -070084bool IsGLSL130OrNewer(ShShaderOutput output)
85{
Jamie Madillacb4b812016-11-07 13:50:29 -050086 return (output == SH_GLSL_130_OUTPUT || output == SH_GLSL_140_OUTPUT ||
87 output == SH_GLSL_150_CORE_OUTPUT || output == SH_GLSL_330_CORE_OUTPUT ||
88 output == SH_GLSL_400_CORE_OUTPUT || output == SH_GLSL_410_CORE_OUTPUT ||
89 output == SH_GLSL_420_CORE_OUTPUT || output == SH_GLSL_430_CORE_OUTPUT ||
90 output == SH_GLSL_440_CORE_OUTPUT || output == SH_GLSL_450_CORE_OUTPUT);
Qingqing Dengad0d0792015-04-08 14:25:06 -070091}
92
Qiankun Miao705a9192016-08-29 10:05:27 +080093bool IsGLSL420OrNewer(ShShaderOutput output)
94{
Jamie Madillacb4b812016-11-07 13:50:29 -050095 return (output == SH_GLSL_420_CORE_OUTPUT || output == SH_GLSL_430_CORE_OUTPUT ||
96 output == SH_GLSL_440_CORE_OUTPUT || output == SH_GLSL_450_CORE_OUTPUT);
Qiankun Miao705a9192016-08-29 10:05:27 +080097}
98
Zhenyao Mob7bf7422016-11-08 14:44:05 -080099bool IsGLSL410OrOlder(ShShaderOutput output)
100{
101 return (output == SH_GLSL_130_OUTPUT || output == SH_GLSL_140_OUTPUT ||
102 output == SH_GLSL_150_CORE_OUTPUT || output == SH_GLSL_330_CORE_OUTPUT ||
103 output == SH_GLSL_400_CORE_OUTPUT || output == SH_GLSL_410_CORE_OUTPUT);
104}
105
Qiankun Miao89dd8f32016-11-09 12:59:30 +0000106bool RemoveInvariant(sh::GLenum shaderType,
107 int shaderVersion,
108 ShShaderOutput outputType,
109 ShCompileOptions compileOptions)
110{
111 if ((compileOptions & SH_DONT_REMOVE_INVARIANT_FOR_FRAGMENT_INPUT) == 0 &&
112 shaderType == GL_FRAGMENT_SHADER && IsGLSL420OrNewer(outputType))
113 return true;
114
115 if ((compileOptions & SH_REMOVE_INVARIANT_AND_CENTROID_FOR_ESSL3) != 0 &&
Qiankun Miao41f9f672016-11-16 17:04:36 +0800116 shaderVersion >= 300 && shaderType == GL_VERTEX_SHADER)
Qiankun Miao89dd8f32016-11-09 12:59:30 +0000117 return true;
118
119 return false;
120}
121
Zhenyao Mo7faf1a12014-04-25 18:03:56 -0700122size_t GetGlobalMaxTokenSize(ShShaderSpec spec)
Jamie Madill88f6e942014-02-19 10:27:53 -0500123{
He Yunchao29ab9ff2015-08-06 16:58:30 +0800124 // WebGL defines a max token length of 256, while ES2 leaves max token
Jamie Madill88f6e942014-02-19 10:27:53 -0500125 // size undefined. ES3 defines a max size of 1024 characters.
Zhenyao Modb9b40b2014-10-29 15:00:04 -0700126 switch (spec)
Jamie Madill88f6e942014-02-19 10:27:53 -0500127 {
Jamie Madillacb4b812016-11-07 13:50:29 -0500128 case SH_WEBGL_SPEC:
129 return 256;
130 default:
131 return 1024;
Jamie Madill88f6e942014-02-19 10:27:53 -0500132 }
133}
134
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500135namespace
136{
Zhenyao Modb9b40b2014-10-29 15:00:04 -0700137
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800138class TScopedPoolAllocator
139{
140 public:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500141 TScopedPoolAllocator(TPoolAllocator *allocator) : mAllocator(allocator)
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800142 {
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400143 mAllocator->push();
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000144 SetGlobalPoolAllocator(mAllocator);
145 }
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800146 ~TScopedPoolAllocator()
147 {
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000148 SetGlobalPoolAllocator(NULL);
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400149 mAllocator->pop();
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000150 }
151
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800152 private:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500153 TPoolAllocator *mAllocator;
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400154};
155
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800156class TScopedSymbolTableLevel
157{
158 public:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500159 TScopedSymbolTableLevel(TSymbolTable *table) : mTable(table)
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800160 {
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400161 ASSERT(mTable->atBuiltInLevel());
162 mTable->push();
163 }
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800164 ~TScopedSymbolTableLevel()
165 {
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400166 while (!mTable->atBuiltInLevel())
167 mTable->pop();
168 }
169
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800170 private:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500171 TSymbolTable *mTable;
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000172};
Zhenyao Modb9b40b2014-10-29 15:00:04 -0700173
174int MapSpecToShaderVersion(ShShaderSpec spec)
175{
176 switch (spec)
177 {
Jamie Madillacb4b812016-11-07 13:50:29 -0500178 case SH_GLES2_SPEC:
179 case SH_WEBGL_SPEC:
180 return 100;
181 case SH_GLES3_SPEC:
182 case SH_WEBGL2_SPEC:
183 return 300;
184 case SH_GLES3_1_SPEC:
185 case SH_WEBGL3_SPEC:
186 return 310;
187 default:
188 UNREACHABLE();
189 return 0;
Zhenyao Modb9b40b2014-10-29 15:00:04 -0700190 }
191}
192
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000193} // namespace
194
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800195TShHandleBase::TShHandleBase()
196{
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000197 allocator.push();
198 SetGlobalPoolAllocator(&allocator);
199}
200
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800201TShHandleBase::~TShHandleBase()
202{
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000203 SetGlobalPoolAllocator(NULL);
204 allocator.popAll();
205}
206
Jamie Madill183bde52014-07-02 15:31:19 -0400207TCompiler::TCompiler(sh::GLenum type, ShShaderSpec spec, ShShaderOutput output)
Kenneth Russellbccc65d2016-07-19 16:48:43 -0700208 : variablesCollected(false),
209 shaderType(type),
zmo@google.comf420c422011-09-12 18:27:59 +0000210 shaderSpec(spec),
Jamie Madill68fe74a2014-05-27 12:56:01 -0400211 outputType(output),
Jamie Madilleb1a0102013-07-08 13:31:38 -0400212 maxUniformVectors(0),
213 maxExpressionComplexity(0),
214 maxCallStackDepth(0),
Olli Etuaho19d1dc92016-03-08 17:18:46 +0200215 maxFunctionParameters(0),
shannon.woods%transgaming.com@gtempaccount.comcbb6b6a2013-04-13 03:27:47 +0000216 fragmentPrecisionHigh(false),
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000217 clampingStrategy(SH_CLAMP_WITH_CLAMP_INTRINSIC),
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200218 builtInFunctionEmulator(),
Olli Etuaho77ba4082016-12-16 12:01:18 +0000219 mDiagnostics(infoSink.info),
Corentin Wallezd4b50542015-09-28 12:19:26 -0700220 mSourcePath(NULL),
Martin Radev802abe02016-08-04 17:48:32 +0300221 mComputeShaderLocalSizeDeclared(false),
Corentin Wallezd4b50542015-09-28 12:19:26 -0700222 mTemporaryIndex(0)
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000223{
Martin Radev802abe02016-08-04 17:48:32 +0300224 mComputeShaderLocalSize.fill(1);
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000225}
226
227TCompiler::~TCompiler()
228{
229}
230
Qiankun Miao7ebb97f2016-09-08 18:01:50 +0800231bool TCompiler::shouldRunLoopAndIndexingValidation(ShCompileOptions compileOptions) const
Olli Etuaho5d91dda2015-06-18 15:47:46 +0300232{
233 // If compiling an ESSL 1.00 shader for WebGL, or if its been requested through the API,
234 // validate loop and indexing as well (to verify that the shader only uses minimal functionality
235 // of ESSL 1.00 as in Appendix A of the spec).
236 return (IsWebGLBasedSpec(shaderSpec) && shaderVersion == 100) ||
237 (compileOptions & SH_VALIDATE_LOOP_INDEXING);
238}
239
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500240bool TCompiler::Init(const ShBuiltInResources &resources)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000241{
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500242 shaderVersion = 100;
243 maxUniformVectors = (shaderType == GL_VERTEX_SHADER) ? resources.MaxVertexUniformVectors
244 : resources.MaxFragmentUniformVectors;
Jamie Madilleb1a0102013-07-08 13:31:38 -0400245 maxExpressionComplexity = resources.MaxExpressionComplexity;
Olli Etuaho19d1dc92016-03-08 17:18:46 +0200246 maxCallStackDepth = resources.MaxCallStackDepth;
247 maxFunctionParameters = resources.MaxFunctionParameters;
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400248
249 SetGlobalPoolAllocator(&allocator);
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000250
alokp@chromium.org07620a52010-09-23 17:53:56 +0000251 // Generate built-in symbol table.
252 if (!InitBuiltInSymbolTable(resources))
253 return false;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000254 InitExtensionBehavior(resources, extensionBehavior);
shannon.woods%transgaming.com@gtempaccount.comcbb6b6a2013-04-13 03:27:47 +0000255 fragmentPrecisionHigh = resources.FragmentPrecisionHigh == 1;
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000256
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000257 arrayBoundsClamper.SetClampingStrategy(resources.ArrayIndexClampingStrategy);
258 clampingStrategy = resources.ArrayIndexClampingStrategy;
259
daniel@transgaming.comc23f4612012-11-28 19:42:57 +0000260 hashFunction = resources.HashFunction;
261
alokp@chromium.org07620a52010-09-23 17:53:56 +0000262 return true;
263}
264
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100265TIntermBlock *TCompiler::compileTreeForTesting(const char *const shaderStrings[],
266 size_t numStrings,
267 ShCompileOptions compileOptions)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000268{
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200269 return compileTreeImpl(shaderStrings, numStrings, compileOptions);
270}
271
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100272TIntermBlock *TCompiler::compileTreeImpl(const char *const shaderStrings[],
273 size_t numStrings,
274 const ShCompileOptions compileOptions)
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200275{
alokp@chromium.org07620a52010-09-23 17:53:56 +0000276 clearResults();
277
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200278 ASSERT(numStrings > 0);
279 ASSERT(GetGlobalPoolAllocator());
alokp@chromium.org07620a52010-09-23 17:53:56 +0000280
David Yen0fbd1282015-02-02 14:46:09 -0800281 // Reset the extension behavior for each compilation unit.
282 ResetExtensionBehavior(extensionBehavior);
283
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000284 // First string is path of source file if flag is set. The actual source follows.
shannon.woods@transgaming.comd64b3da2013-02-28 23:19:26 +0000285 size_t firstSource = 0;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000286 if (compileOptions & SH_SOURCE_PATH)
287 {
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200288 mSourcePath = shaderStrings[0];
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000289 ++firstSource;
290 }
291
Olli Etuahof119a262016-08-19 15:54:22 +0300292 TParseContext parseContext(symbolTable, extensionBehavior, shaderType, shaderSpec,
Olli Etuaho77ba4082016-12-16 12:01:18 +0000293 compileOptions, true, &mDiagnostics, getResources());
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200294
Olli Etuahoa6996682015-10-12 14:32:30 +0300295 parseContext.setFragmentPrecisionHighOnESSL1(fragmentPrecisionHigh);
Alok Priyadarshi8156b6b2013-09-23 14:56:58 -0400296 SetGlobalParseContext(&parseContext);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000297
298 // We preserve symbols at the built-in level from compile-to-compile.
299 // Start pushing the user-defined symbols at global level.
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400300 TScopedSymbolTableLevel scopedSymbolLevel(&symbolTable);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000301
302 // Parse shader.
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500303 bool success = (PaParseStrings(numStrings - firstSource, &shaderStrings[firstSource], nullptr,
304 &parseContext) == 0) &&
305 (parseContext.getTreeRoot() != nullptr);
shannon.woods%transgaming.com@gtempaccount.com0bbed382013-04-13 03:38:07 +0000306
shannon.woods%transgaming.com@gtempaccount.com5524db02013-04-13 03:38:16 +0000307 shaderVersion = parseContext.getShaderVersion();
Zhenyao Modb9b40b2014-10-29 15:00:04 -0700308 if (success && MapSpecToShaderVersion(shaderSpec) < shaderVersion)
309 {
Olli Etuaho77ba4082016-12-16 12:01:18 +0000310 mDiagnostics.globalError("unsupported shader version");
Zhenyao Modb9b40b2014-10-29 15:00:04 -0700311 success = false;
312 }
shannon.woods%transgaming.com@gtempaccount.com0bbed382013-04-13 03:38:07 +0000313
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100314 TIntermBlock *root = nullptr;
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200315
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800316 if (success)
317 {
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700318 mPragma = parseContext.pragma();
Kenneth Russell8bad46d2016-07-01 19:52:52 -0700319 symbolTable.setGlobalInvariant(mPragma.stdgl.invariantAll);
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700320
Martin Radev802abe02016-08-04 17:48:32 +0300321 mComputeShaderLocalSizeDeclared = parseContext.isComputeShaderLocalSizeDeclared();
322 mComputeShaderLocalSize = parseContext.getComputeShaderLocalSize();
323
Olli Etuaho09b04a22016-12-15 13:30:26 +0000324 mNumViews = parseContext.getNumViews();
325
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400326 root = parseContext.getTreeRoot();
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000327
Olli Etuahoa6996682015-10-12 14:32:30 +0300328 // Highp might have been auto-enabled based on shader version
329 fragmentPrecisionHigh = parseContext.getFragmentPrecisionHigh();
330
Olli Etuaho09b04a22016-12-15 13:30:26 +0000331 if (success && (IsWebGLBasedSpec(shaderSpec) &&
332 IsExtensionEnabled(extensionBehavior, "GL_OVR_multiview") &&
333 IsExtensionEnabled(extensionBehavior, "GL_OVR_multiview2")))
334 {
335 // Can't enable both extensions at the same time.
336 mDiagnostics.globalError("Can't enable both OVR_multiview and OVR_multiview2");
337 success = false;
338 }
339
Jamie Madill6654bc92014-03-26 14:01:57 -0400340 // Disallow expressions deemed too complex.
341 if (success && (compileOptions & SH_LIMIT_EXPRESSION_COMPLEXITY))
342 success = limitExpressionComplexity(root);
343
Corentin Wallez71d147f2015-02-11 11:15:24 -0800344 // Create the function DAG and check there is no recursion
zmo@google.comb1762df2011-07-30 02:04:23 +0000345 if (success)
Corentin Wallez71d147f2015-02-11 11:15:24 -0800346 success = initCallDag(root);
347
348 if (success && (compileOptions & SH_LIMIT_CALL_STACK_DEPTH))
349 success = checkCallDepth();
350
351 // Checks which functions are used and if "main" exists
352 if (success)
353 {
354 functionMetadata.clear();
355 functionMetadata.resize(mCallDag.size());
356 success = tagUsedFunctions();
357 }
zmo@google.comb1762df2011-07-30 02:04:23 +0000358
Corentin Walleza094a8a2015-04-07 11:53:06 -0700359 if (success && !(compileOptions & SH_DONT_PRUNE_UNUSED_FUNCTIONS))
360 success = pruneUnusedFunctions(root);
361
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500362 // Prune empty declarations to work around driver bugs and to keep declaration output
363 // simple.
Olli Etuahoc6833112015-04-22 15:15:54 +0300364 if (success)
365 PruneEmptyDeclarations(root);
366
Jamie Madill183bde52014-07-02 15:31:19 -0400367 if (success && shaderVersion == 300 && shaderType == GL_FRAGMENT_SHADER)
Jamie Madill05a80ce2013-06-20 11:55:49 -0400368 success = validateOutputs(root);
369
Olli Etuaho5d91dda2015-06-18 15:47:46 +0300370 if (success && shouldRunLoopAndIndexingValidation(compileOptions))
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000371 success = validateLimitations(root);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000372
Olli Etuaho09b04a22016-12-15 13:30:26 +0000373 bool multiview2 = IsExtensionEnabled(extensionBehavior, "GL_OVR_multiview2");
374 if (success && compileResources.OVR_multiview && IsWebGLBasedSpec(shaderSpec) &&
375 (IsExtensionEnabled(extensionBehavior, "GL_OVR_multiview") || multiview2))
Olli Etuaho01d0ad02017-01-22 14:51:23 -0800376 success = ValidateMultiviewWebGL(root, shaderType, symbolTable, shaderVersion,
377 multiview2, &mDiagnostics);
Olli Etuaho09b04a22016-12-15 13:30:26 +0000378
Jamie Madilld5696192016-10-06 11:09:24 -0400379 // Fail compilation if precision emulation not supported.
380 if (success && getResources().WEBGL_debug_shader_precision &&
381 getPragma().debugShaderPrecision)
382 {
383 if (!EmulatePrecision::SupportedInLanguage(outputType))
384 {
Olli Etuaho77ba4082016-12-16 12:01:18 +0000385 mDiagnostics.globalError("Precision emulation not supported for this output type.");
Jamie Madilld5696192016-10-06 11:09:24 -0400386 success = false;
387 }
388 }
389
zmo@google.com32e97312011-08-24 01:03:11 +0000390 // Built-in function emulation needs to happen after validateLimitations pass.
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200391 if (success)
392 {
Jamie Madill438dbcf2016-06-17 14:20:05 -0400393 // TODO(jmadill): Remove global pool allocator.
394 GetGlobalPoolAllocator()->lock();
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200395 initBuiltInFunctionEmulator(&builtInFunctionEmulator, compileOptions);
Jamie Madill438dbcf2016-06-17 14:20:05 -0400396 GetGlobalPoolAllocator()->unlock();
Olli Etuahodfa75e82017-01-23 09:43:06 -0800397 builtInFunctionEmulator.markBuiltInFunctionsForEmulation(root);
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200398 }
zmo@google.com32e97312011-08-24 01:03:11 +0000399
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000400 // Clamping uniform array bounds needs to happen after validateLimitations pass.
401 if (success && (compileOptions & SH_CLAMP_INDIRECT_ARRAY_BOUNDS))
402 arrayBoundsClamper.MarkIndirectArrayBoundsForClamping(root);
403
Ian Ewell924b7de2016-01-21 13:54:28 -0500404 // gl_Position is always written in compatibility output mode
405 if (success && shaderType == GL_VERTEX_SHADER &&
406 ((compileOptions & SH_INIT_GL_POSITION) ||
407 (outputType == SH_GLSL_COMPATIBILITY_OUTPUT)))
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800408 initializeGLPosition(root);
Zhenyao Moac44cd22013-09-23 14:57:09 -0400409
Corentin Wallezd4b50542015-09-28 12:19:26 -0700410 // This pass might emit short circuits so keep it before the short circuit unfolding
411 if (success && (compileOptions & SH_REWRITE_DO_WHILE_LOOPS))
412 RewriteDoWhile(root, getTemporaryIndex());
413
Qiankun Miao09cfac62016-09-06 17:25:16 +0800414 if (success && (compileOptions & SH_ADD_AND_TRUE_TO_LOOP_CONDITION))
415 sh::AddAndTrueToLoopCondition(root);
416
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800417 if (success && (compileOptions & SH_UNFOLD_SHORT_CIRCUIT))
418 {
Zhenyao Mo7cab38b2013-10-15 12:59:30 -0700419 UnfoldShortCircuitAST unfoldShortCircuit;
420 root->traverse(&unfoldShortCircuit);
421 unfoldShortCircuit.updateTree();
422 }
423
Olli Etuaho5c407bb2015-06-01 12:20:39 +0300424 if (success && (compileOptions & SH_REMOVE_POW_WITH_CONSTANT_EXPONENT))
425 {
426 RemovePow(root);
427 }
428
Olli Etuaho4dfe8092015-08-21 17:44:35 +0300429 if (success && shouldCollectVariables(compileOptions))
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800430 {
Zhenyao Mo74da9f22013-09-23 14:57:01 -0400431 collectVariables(root);
Qin Jiajia7835b522016-10-08 11:20:17 +0800432 if (compileOptions & SH_USE_UNUSED_STANDARD_SHARED_BLOCKS)
433 {
434 useAllMembersInUnusedStandardAndSharedBlocks(root);
435 }
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800436 if (compileOptions & SH_ENFORCE_PACKING_RESTRICTIONS)
437 {
gman@chromium.org8d804792012-10-17 21:33:48 +0000438 success = enforcePackingRestrictions();
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800439 if (!success)
440 {
Olli Etuaho77ba4082016-12-16 12:01:18 +0000441 mDiagnostics.globalError("too many uniforms");
gman@chromium.org8d804792012-10-17 21:33:48 +0000442 }
443 }
Zhenyao Mo72111912016-07-20 17:45:56 -0700444 if (success && (compileOptions & SH_INIT_OUTPUT_VARIABLES))
445 {
Olli Etuaho27776e32016-07-22 14:00:56 +0300446 initializeOutputVariables(root);
Zhenyao Mo72111912016-07-20 17:45:56 -0700447 }
gman@chromium.org8d804792012-10-17 21:33:48 +0000448 }
zmo@google.comfd747b82011-04-23 01:30:07 +0000449
Yuly Novikov817232e2017-02-22 18:36:10 -0500450 // Removing invariant declarations must be done after collecting variables.
451 // Otherwise, built-in invariant declarations don't apply.
452 if (success && RemoveInvariant(shaderType, shaderVersion, outputType, compileOptions))
453 sh::RemoveInvariantDeclaration(root);
454
Zhenyao Mocd68fe72014-07-11 10:45:44 -0700455 if (success && (compileOptions & SH_SCALARIZE_VEC_AND_MAT_CONSTRUCTOR_ARGS))
456 {
Olli Etuahob990b552016-10-27 12:29:17 +0100457 ScalarizeVecAndMatConstructorArgs(root, shaderType, fragmentPrecisionHigh,
458 &mTemporaryIndex);
Zhenyao Mocd68fe72014-07-11 10:45:44 -0700459 }
460
Zhenyao Moe740add2014-07-18 17:01:01 -0700461 if (success && (compileOptions & SH_REGENERATE_STRUCT_NAMES))
462 {
463 RegenerateStructNames gen(symbolTable, shaderVersion);
464 root->traverse(&gen);
465 }
Olli Etuaho3d932d82016-04-12 11:10:30 +0300466
Zhenyao Mo4e94fea2016-08-09 14:31:37 -0700467 if (success && shaderType == GL_FRAGMENT_SHADER && shaderVersion == 100 &&
468 compileResources.EXT_draw_buffers && compileResources.MaxDrawBuffers > 1 &&
469 IsExtensionEnabled(extensionBehavior, "GL_EXT_draw_buffers"))
470 {
471 EmulateGLFragColorBroadcast(root, compileResources.MaxDrawBuffers, &outputVariables);
472 }
473
Olli Etuaho3d932d82016-04-12 11:10:30 +0300474 if (success)
475 {
476 DeferGlobalInitializers(root);
477 }
alokp@chromium.org07620a52010-09-23 17:53:56 +0000478 }
479
Zhenyao Mo7faf1a12014-04-25 18:03:56 -0700480 SetGlobalParseContext(NULL);
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200481 if (success)
482 return root;
483
484 return NULL;
485}
486
Qiankun Miao7ebb97f2016-09-08 18:01:50 +0800487bool TCompiler::compile(const char *const shaderStrings[],
488 size_t numStrings,
489 ShCompileOptions compileOptionsIn)
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200490{
Corentin Wallez28b65282016-06-16 07:24:50 -0700491#if defined(ANGLE_ENABLE_FUZZER_CORPUS_OUTPUT)
492 DumpFuzzerCase(shaderStrings, numStrings, shaderType, shaderSpec, outputType, compileOptionsIn);
493#endif // defined(ANGLE_ENABLE_FUZZER_CORPUS_OUTPUT)
494
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200495 if (numStrings == 0)
496 return true;
497
Qiankun Miao7ebb97f2016-09-08 18:01:50 +0800498 ShCompileOptions compileOptions = compileOptionsIn;
Kenneth Russellbccc65d2016-07-19 16:48:43 -0700499
500 // Apply key workarounds.
501 if (shouldFlattenPragmaStdglInvariantAll())
502 {
503 // This should be harmless to do in all cases, but for the moment, do it only conditionally.
504 compileOptions |= SH_FLATTEN_PRAGMA_STDGL_INVARIANT_ALL;
505 }
506
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200507 TScopedPoolAllocator scopedAlloc(&allocator);
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100508 TIntermBlock *root = compileTreeImpl(shaderStrings, numStrings, compileOptions);
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200509
510 if (root)
511 {
512 if (compileOptions & SH_INTERMEDIATE_TREE)
513 TIntermediate::outputTree(root, infoSink.info);
514
515 if (compileOptions & SH_OBJECT_CODE)
516 translate(root, compileOptions);
517
518 // The IntermNode tree doesn't need to be deleted here, since the
519 // memory will be freed in a big chunk by the PoolAllocator.
520 return true;
521 }
522 return false;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000523}
524
Nicolas Capens49a88872013-06-20 09:54:03 -0400525bool TCompiler::InitBuiltInSymbolTable(const ShBuiltInResources &resources)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000526{
Olli Etuaho28cb0362016-11-22 15:42:37 +0000527 if (resources.MaxDrawBuffers < 1)
528 {
529 return false;
530 }
531 if (resources.EXT_blend_func_extended && resources.MaxDualSourceDrawBuffers < 1)
532 {
533 return false;
534 }
535
shannon.woods%transgaming.com@gtempaccount.com18b4c4b2013-04-13 03:31:40 +0000536 compileResources = resources;
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400537 setResourceString();
shannonwoods@chromium.org2ac0be92013-05-30 00:02:27 +0000538
Nicolas Capens49a88872013-06-20 09:54:03 -0400539 assert(symbolTable.isEmpty());
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500540 symbolTable.push(); // COMMON_BUILTINS
541 symbolTable.push(); // ESSL1_BUILTINS
542 symbolTable.push(); // ESSL3_BUILTINS
543 symbolTable.push(); // ESSL3_1_BUILTINS
shannonwoods@chromium.org2ac0be92013-05-30 00:02:27 +0000544
Nicolas Capens49a88872013-06-20 09:54:03 -0400545 TPublicType integer;
Martin Radev2cc85b32016-08-05 16:22:53 +0300546 integer.initializeBasicType(EbtInt);
Nicolas Capens49a88872013-06-20 09:54:03 -0400547
548 TPublicType floatingPoint;
Martin Radev2cc85b32016-08-05 16:22:53 +0300549 floatingPoint.initializeBasicType(EbtFloat);
Nicolas Capens49a88872013-06-20 09:54:03 -0400550
Jamie Madillacb4b812016-11-07 13:50:29 -0500551 switch (shaderType)
Nicolas Capens49a88872013-06-20 09:54:03 -0400552 {
Jamie Madillacb4b812016-11-07 13:50:29 -0500553 case GL_FRAGMENT_SHADER:
554 symbolTable.setDefaultPrecision(integer, EbpMedium);
555 break;
556 case GL_VERTEX_SHADER:
557 symbolTable.setDefaultPrecision(integer, EbpHigh);
558 symbolTable.setDefaultPrecision(floatingPoint, EbpHigh);
559 break;
560 case GL_COMPUTE_SHADER:
561 symbolTable.setDefaultPrecision(integer, EbpHigh);
562 symbolTable.setDefaultPrecision(floatingPoint, EbpHigh);
563 break;
564 default:
565 assert(false && "Language not supported");
Nicolas Capens49a88872013-06-20 09:54:03 -0400566 }
Olli Etuaho183d7e22015-11-20 15:59:09 +0200567 // Set defaults for sampler types that have default precision, even those that are
Zhenyao Moa5a1dfc2013-09-23 14:57:03 -0400568 // only available if an extension exists.
Olli Etuaho183d7e22015-11-20 15:59:09 +0200569 // New sampler types in ESSL3 don't have default precision. ESSL1 types do.
570 initSamplerDefaultPrecision(EbtSampler2D);
571 initSamplerDefaultPrecision(EbtSamplerCube);
572 // SamplerExternalOES is specified in the extension to have default precision.
573 initSamplerDefaultPrecision(EbtSamplerExternalOES);
574 // It isn't specified whether Sampler2DRect has default precision.
575 initSamplerDefaultPrecision(EbtSampler2DRect);
Nicolas Capens49a88872013-06-20 09:54:03 -0400576
Jamie Madill1b452142013-07-12 14:51:11 -0400577 InsertBuiltInFunctions(shaderType, shaderSpec, resources, symbolTable);
Nicolas Capens49a88872013-06-20 09:54:03 -0400578
579 IdentifyBuiltIns(shaderType, shaderSpec, resources, symbolTable);
580
581 return true;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000582}
583
Olli Etuaho183d7e22015-11-20 15:59:09 +0200584void TCompiler::initSamplerDefaultPrecision(TBasicType samplerType)
585{
586 ASSERT(samplerType > EbtGuardSamplerBegin && samplerType < EbtGuardSamplerEnd);
587 TPublicType sampler;
Martin Radev2cc85b32016-08-05 16:22:53 +0300588 sampler.initializeBasicType(samplerType);
Olli Etuaho183d7e22015-11-20 15:59:09 +0200589 symbolTable.setDefaultPrecision(sampler, EbpLow);
590}
591
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400592void TCompiler::setResourceString()
593{
594 std::ostringstream strstream;
Geoff Langb66a9092016-05-16 15:59:14 -0400595
596 // clang-format off
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400597 strstream << ":MaxVertexAttribs:" << compileResources.MaxVertexAttribs
Jamie Madillacb4b812016-11-07 13:50:29 -0500598 << ":MaxVertexUniformVectors:" << compileResources.MaxVertexUniformVectors
599 << ":MaxVaryingVectors:" << compileResources.MaxVaryingVectors
600 << ":MaxVertexTextureImageUnits:" << compileResources.MaxVertexTextureImageUnits
601 << ":MaxCombinedTextureImageUnits:" << compileResources.MaxCombinedTextureImageUnits
602 << ":MaxTextureImageUnits:" << compileResources.MaxTextureImageUnits
603 << ":MaxFragmentUniformVectors:" << compileResources.MaxFragmentUniformVectors
604 << ":MaxDrawBuffers:" << compileResources.MaxDrawBuffers
605 << ":OES_standard_derivatives:" << compileResources.OES_standard_derivatives
606 << ":OES_EGL_image_external:" << compileResources.OES_EGL_image_external
607 << ":OES_EGL_image_external_essl3:" << compileResources.OES_EGL_image_external_essl3
608 << ":NV_EGL_stream_consumer_external:" << compileResources.NV_EGL_stream_consumer_external
609 << ":ARB_texture_rectangle:" << compileResources.ARB_texture_rectangle
610 << ":EXT_draw_buffers:" << compileResources.EXT_draw_buffers
611 << ":FragmentPrecisionHigh:" << compileResources.FragmentPrecisionHigh
612 << ":MaxExpressionComplexity:" << compileResources.MaxExpressionComplexity
613 << ":MaxCallStackDepth:" << compileResources.MaxCallStackDepth
614 << ":MaxFunctionParameters:" << compileResources.MaxFunctionParameters
615 << ":EXT_blend_func_extended:" << compileResources.EXT_blend_func_extended
616 << ":EXT_frag_depth:" << compileResources.EXT_frag_depth
617 << ":EXT_shader_texture_lod:" << compileResources.EXT_shader_texture_lod
618 << ":EXT_shader_framebuffer_fetch:" << compileResources.EXT_shader_framebuffer_fetch
619 << ":NV_shader_framebuffer_fetch:" << compileResources.NV_shader_framebuffer_fetch
620 << ":ARM_shader_framebuffer_fetch:" << compileResources.ARM_shader_framebuffer_fetch
621 << ":MaxVertexOutputVectors:" << compileResources.MaxVertexOutputVectors
622 << ":MaxFragmentInputVectors:" << compileResources.MaxFragmentInputVectors
623 << ":MinProgramTexelOffset:" << compileResources.MinProgramTexelOffset
624 << ":MaxProgramTexelOffset:" << compileResources.MaxProgramTexelOffset
625 << ":MaxDualSourceDrawBuffers:" << compileResources.MaxDualSourceDrawBuffers
626 << ":NV_draw_buffers:" << compileResources.NV_draw_buffers
627 << ":WEBGL_debug_shader_precision:" << compileResources.WEBGL_debug_shader_precision
628 << ":MaxImageUnits:" << compileResources.MaxImageUnits
629 << ":MaxVertexImageUniforms:" << compileResources.MaxVertexImageUniforms
630 << ":MaxFragmentImageUniforms:" << compileResources.MaxFragmentImageUniforms
631 << ":MaxComputeImageUniforms:" << compileResources.MaxComputeImageUniforms
632 << ":MaxCombinedImageUniforms:" << compileResources.MaxCombinedImageUniforms
633 << ":MaxCombinedShaderOutputResources:" << compileResources.MaxCombinedShaderOutputResources
634 << ":MaxComputeWorkGroupCountX:" << compileResources.MaxComputeWorkGroupCount[0]
635 << ":MaxComputeWorkGroupCountY:" << compileResources.MaxComputeWorkGroupCount[1]
636 << ":MaxComputeWorkGroupCountZ:" << compileResources.MaxComputeWorkGroupCount[2]
637 << ":MaxComputeWorkGroupSizeX:" << compileResources.MaxComputeWorkGroupSize[0]
638 << ":MaxComputeWorkGroupSizeY:" << compileResources.MaxComputeWorkGroupSize[1]
639 << ":MaxComputeWorkGroupSizeZ:" << compileResources.MaxComputeWorkGroupSize[2]
640 << ":MaxComputeUniformComponents:" << compileResources.MaxComputeUniformComponents
641 << ":MaxComputeTextureImageUnits:" << compileResources.MaxComputeTextureImageUnits
642 << ":MaxComputeAtomicCounters:" << compileResources.MaxComputeAtomicCounters
643 << ":MaxComputeAtomicCounterBuffers:" << compileResources.MaxComputeAtomicCounterBuffers
644 << ":MaxVertexAtomicCounters:" << compileResources.MaxVertexAtomicCounters
645 << ":MaxFragmentAtomicCounters:" << compileResources.MaxFragmentAtomicCounters
646 << ":MaxCombinedAtomicCounters:" << compileResources.MaxCombinedAtomicCounters
647 << ":MaxAtomicCounterBindings:" << compileResources.MaxAtomicCounterBindings
648 << ":MaxVertexAtomicCounterBuffers:" << compileResources.MaxVertexAtomicCounterBuffers
649 << ":MaxFragmentAtomicCounterBuffers:" << compileResources.MaxFragmentAtomicCounterBuffers
650 << ":MaxCombinedAtomicCounterBuffers:" << compileResources.MaxCombinedAtomicCounterBuffers
651 << ":MaxAtomicCounterBufferSize:" << compileResources.MaxAtomicCounterBufferSize;
Geoff Langb66a9092016-05-16 15:59:14 -0400652 // clang-format on
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400653
654 builtInResourcesString = strstream.str();
655}
656
alokp@chromium.org07620a52010-09-23 17:53:56 +0000657void TCompiler::clearResults()
658{
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000659 arrayBoundsClamper.Cleanup();
alokp@chromium.org07620a52010-09-23 17:53:56 +0000660 infoSink.info.erase();
661 infoSink.obj.erase();
662 infoSink.debug.erase();
Olli Etuaho77ba4082016-12-16 12:01:18 +0000663 mDiagnostics.resetErrorCount();
alokp@chromium.org07620a52010-09-23 17:53:56 +0000664
Jamie Madilled27c722014-07-02 15:31:23 -0400665 attributes.clear();
666 outputVariables.clear();
alokp@chromium.org07620a52010-09-23 17:53:56 +0000667 uniforms.clear();
Jamie Madill23a8a432014-07-09 13:27:42 -0400668 expandedUniforms.clear();
Zhenyao Mod2d340b2013-09-23 14:57:05 -0400669 varyings.clear();
Jamie Madilled27c722014-07-02 15:31:23 -0400670 interfaceBlocks.clear();
Kenneth Russellbccc65d2016-07-19 16:48:43 -0700671 variablesCollected = false;
zmo@google.coma3b4ab42011-09-16 00:53:26 +0000672
Olli Etuaho09b04a22016-12-15 13:30:26 +0000673 mNumViews = -1;
674
Olli Etuahodfa75e82017-01-23 09:43:06 -0800675 builtInFunctionEmulator.cleanup();
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +0000676
677 nameMap.clear();
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200678
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500679 mSourcePath = NULL;
Corentin Wallezd4b50542015-09-28 12:19:26 -0700680 mTemporaryIndex = 0;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000681}
682
Corentin Wallez71d147f2015-02-11 11:15:24 -0800683bool TCompiler::initCallDag(TIntermNode *root)
zmo@google.comb1762df2011-07-30 02:04:23 +0000684{
Corentin Wallez71d147f2015-02-11 11:15:24 -0800685 mCallDag.clear();
686
Olli Etuaho77ba4082016-12-16 12:01:18 +0000687 switch (mCallDag.init(root, &mDiagnostics))
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800688 {
Jamie Madillacb4b812016-11-07 13:50:29 -0500689 case CallDAG::INITDAG_SUCCESS:
690 return true;
691 case CallDAG::INITDAG_RECURSION:
Jamie Madillacb4b812016-11-07 13:50:29 -0500692 case CallDAG::INITDAG_UNDEFINED:
Olli Etuaho77ba4082016-12-16 12:01:18 +0000693 // Error message has already been written out.
694 ASSERT(mDiagnostics.numErrors() > 0);
Jamie Madillacb4b812016-11-07 13:50:29 -0500695 return false;
Corentin Wallez71d147f2015-02-11 11:15:24 -0800696 }
697
698 UNREACHABLE();
699 return true;
700}
701
702bool TCompiler::checkCallDepth()
703{
704 std::vector<int> depths(mCallDag.size());
705
706 for (size_t i = 0; i < mCallDag.size(); i++)
707 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500708 int depth = 0;
Corentin Wallez71d147f2015-02-11 11:15:24 -0800709 auto &record = mCallDag.getRecordFromIndex(i);
710
711 for (auto &calleeIndex : record.callees)
712 {
713 depth = std::max(depth, depths[calleeIndex] + 1);
714 }
715
716 depths[i] = depth;
717
718 if (depth >= maxCallStackDepth)
719 {
720 // Trace back the function chain to have a meaningful info log.
Olli Etuaho77ba4082016-12-16 12:01:18 +0000721 std::stringstream errorStream;
722 errorStream << "Call stack too deep (larger than " << maxCallStackDepth
723 << ") with the following call chain: " << record.name;
Corentin Wallez71d147f2015-02-11 11:15:24 -0800724
Cooper Partin4d61f7e2015-08-12 10:56:50 -0700725 int currentFunction = static_cast<int>(i);
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500726 int currentDepth = depth;
Corentin Wallez71d147f2015-02-11 11:15:24 -0800727
728 while (currentFunction != -1)
729 {
Olli Etuaho77ba4082016-12-16 12:01:18 +0000730 errorStream << " -> " << mCallDag.getRecordFromIndex(currentFunction).name;
Corentin Wallez71d147f2015-02-11 11:15:24 -0800731
732 int nextFunction = -1;
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500733 for (auto &calleeIndex : mCallDag.getRecordFromIndex(currentFunction).callees)
Corentin Wallez71d147f2015-02-11 11:15:24 -0800734 {
735 if (depths[calleeIndex] == currentDepth - 1)
736 {
737 currentDepth--;
738 nextFunction = calleeIndex;
739 }
740 }
741
742 currentFunction = nextFunction;
743 }
744
Olli Etuaho77ba4082016-12-16 12:01:18 +0000745 std::string errorStr = errorStream.str();
746 mDiagnostics.globalError(errorStr.c_str());
747
Corentin Wallez71d147f2015-02-11 11:15:24 -0800748 return false;
749 }
750 }
751
752 return true;
753}
754
755bool TCompiler::tagUsedFunctions()
756{
757 // Search from main, starting from the end of the DAG as it usually is the root.
Cooper Partin4d61f7e2015-08-12 10:56:50 -0700758 for (size_t i = mCallDag.size(); i-- > 0;)
Corentin Wallez71d147f2015-02-11 11:15:24 -0800759 {
760 if (mCallDag.getRecordFromIndex(i).name == "main(")
761 {
762 internalTagUsedFunction(i);
763 return true;
764 }
765 }
766
Olli Etuaho77ba4082016-12-16 12:01:18 +0000767 mDiagnostics.globalError("Missing main()");
Corentin Wallez71d147f2015-02-11 11:15:24 -0800768 return false;
769}
770
771void TCompiler::internalTagUsedFunction(size_t index)
772{
773 if (functionMetadata[index].used)
774 {
775 return;
776 }
777
778 functionMetadata[index].used = true;
779
780 for (int calleeIndex : mCallDag.getRecordFromIndex(index).callees)
781 {
782 internalTagUsedFunction(calleeIndex);
zmo@google.comb1762df2011-07-30 02:04:23 +0000783 }
784}
785
Corentin Walleza094a8a2015-04-07 11:53:06 -0700786// A predicate for the stl that returns if a top-level node is unused
787class TCompiler::UnusedPredicate
788{
789 public:
790 UnusedPredicate(const CallDAG *callDag, const std::vector<FunctionMetadata> *metadatas)
Jamie Madillacb4b812016-11-07 13:50:29 -0500791 : mCallDag(callDag), mMetadatas(metadatas)
Corentin Walleza094a8a2015-04-07 11:53:06 -0700792 {
793 }
794
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500795 bool operator()(TIntermNode *node)
Corentin Walleza094a8a2015-04-07 11:53:06 -0700796 {
Olli Etuaho16c745a2017-01-16 17:02:27 +0000797 const TIntermFunctionPrototype *asFunctionPrototype = node->getAsFunctionPrototypeNode();
798 const TIntermFunctionDefinition *asFunctionDefinition = node->getAsFunctionDefinition();
Corentin Walleza094a8a2015-04-07 11:53:06 -0700799
Olli Etuaho336b1472016-10-05 16:37:55 +0100800 const TFunctionSymbolInfo *functionInfo = nullptr;
801
Olli Etuaho16c745a2017-01-16 17:02:27 +0000802 if (asFunctionDefinition)
Olli Etuaho336b1472016-10-05 16:37:55 +0100803 {
Olli Etuaho16c745a2017-01-16 17:02:27 +0000804 functionInfo = asFunctionDefinition->getFunctionSymbolInfo();
Olli Etuaho336b1472016-10-05 16:37:55 +0100805 }
Olli Etuaho16c745a2017-01-16 17:02:27 +0000806 else if (asFunctionPrototype)
Olli Etuaho336b1472016-10-05 16:37:55 +0100807 {
Olli Etuaho16c745a2017-01-16 17:02:27 +0000808 functionInfo = asFunctionPrototype->getFunctionSymbolInfo();
Olli Etuaho336b1472016-10-05 16:37:55 +0100809 }
810 if (functionInfo == nullptr)
Corentin Walleza094a8a2015-04-07 11:53:06 -0700811 {
812 return false;
813 }
814
Olli Etuaho336b1472016-10-05 16:37:55 +0100815 size_t callDagIndex = mCallDag->findIndex(functionInfo);
Corentin Walleza094a8a2015-04-07 11:53:06 -0700816 if (callDagIndex == CallDAG::InvalidIndex)
817 {
818 // This happens only for unimplemented prototypes which are thus unused
Olli Etuaho16c745a2017-01-16 17:02:27 +0000819 ASSERT(asFunctionPrototype);
Corentin Walleza094a8a2015-04-07 11:53:06 -0700820 return true;
821 }
822
823 ASSERT(callDagIndex < mMetadatas->size());
824 return !(*mMetadatas)[callDagIndex].used;
825 }
826
827 private:
828 const CallDAG *mCallDag;
829 const std::vector<FunctionMetadata> *mMetadatas;
830};
831
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100832bool TCompiler::pruneUnusedFunctions(TIntermBlock *root)
Corentin Walleza094a8a2015-04-07 11:53:06 -0700833{
Corentin Walleza094a8a2015-04-07 11:53:06 -0700834 UnusedPredicate isUnused(&mCallDag, &functionMetadata);
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100835 TIntermSequence *sequence = root->getSequence();
Corentin Wallezb081e782015-07-20 05:40:04 -0700836
837 if (!sequence->empty())
838 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500839 sequence->erase(std::remove_if(sequence->begin(), sequence->end(), isUnused),
840 sequence->end());
Corentin Wallezb081e782015-07-20 05:40:04 -0700841 }
Corentin Walleza094a8a2015-04-07 11:53:06 -0700842
843 return true;
844}
845
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500846bool TCompiler::validateOutputs(TIntermNode *root)
Jamie Madill05a80ce2013-06-20 11:55:49 -0400847{
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +0300848 ValidateOutputs validateOutputs(getExtensionBehavior(), compileResources.MaxDrawBuffers);
Jamie Madill05a80ce2013-06-20 11:55:49 -0400849 root->traverse(&validateOutputs);
Olli Etuaho77ba4082016-12-16 12:01:18 +0000850 validateOutputs.validate(&mDiagnostics);
851 return (mDiagnostics.numErrors() == 0);
Jamie Madill05a80ce2013-06-20 11:55:49 -0400852}
853
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500854bool TCompiler::validateLimitations(TIntermNode *root)
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800855{
Olli Etuaho77ba4082016-12-16 12:01:18 +0000856 ValidateLimitations validate(shaderType, &mDiagnostics);
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000857 root->traverse(&validate);
Olli Etuaho77ba4082016-12-16 12:01:18 +0000858 return mDiagnostics.numErrors() == 0;
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000859}
860
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500861bool TCompiler::limitExpressionComplexity(TIntermNode *root)
Jamie Madilleb1a0102013-07-08 13:31:38 -0400862{
Jamie Madillacb4b812016-11-07 13:50:29 -0500863 TMaxDepthTraverser traverser(maxExpressionComplexity + 1);
Jamie Madilleb1a0102013-07-08 13:31:38 -0400864 root->traverse(&traverser);
Jamie Madill6654bc92014-03-26 14:01:57 -0400865
866 if (traverser.getMaxDepth() > maxExpressionComplexity)
867 {
Olli Etuaho77ba4082016-12-16 12:01:18 +0000868 mDiagnostics.globalError("Expression too complex.");
Jamie Madill6654bc92014-03-26 14:01:57 -0400869 return false;
870 }
871
Olli Etuaho19d1dc92016-03-08 17:18:46 +0200872 if (!ValidateMaxParameters::validate(root, maxFunctionParameters))
873 {
Olli Etuaho77ba4082016-12-16 12:01:18 +0000874 mDiagnostics.globalError("Function has too many parameters.");
Olli Etuaho19d1dc92016-03-08 17:18:46 +0200875 return false;
876 }
877
Jamie Madilleb1a0102013-07-08 13:31:38 -0400878 return true;
879}
880
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500881void TCompiler::collectVariables(TIntermNode *root)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000882{
Kenneth Russellbccc65d2016-07-19 16:48:43 -0700883 if (!variablesCollected)
884 {
885 sh::CollectVariables collect(&attributes, &outputVariables, &uniforms, &varyings,
Jamie Madillacb4b812016-11-07 13:50:29 -0500886 &interfaceBlocks, hashFunction, symbolTable,
887 extensionBehavior);
Kenneth Russellbccc65d2016-07-19 16:48:43 -0700888 root->traverse(&collect);
Jamie Madill23a8a432014-07-09 13:27:42 -0400889
Kenneth Russellbccc65d2016-07-19 16:48:43 -0700890 // This is for enforcePackingRestriction().
891 sh::ExpandUniforms(uniforms, &expandedUniforms);
892 variablesCollected = true;
893 }
alokp@chromium.org07620a52010-09-23 17:53:56 +0000894}
zmo@google.comfd747b82011-04-23 01:30:07 +0000895
Corentin Wallez1df16022016-10-27 08:16:56 -0400896bool TCompiler::shouldCollectVariables(ShCompileOptions compileOptions)
897{
898 return (compileOptions & SH_VARIABLES) != 0;
899}
900
901bool TCompiler::wereVariablesCollected() const
902{
903 return variablesCollected;
904}
905
gman@chromium.org8d804792012-10-17 21:33:48 +0000906bool TCompiler::enforcePackingRestrictions()
907{
908 VariablePacker packer;
Jamie Madill23a8a432014-07-09 13:27:42 -0400909 return packer.CheckVariablesWithinPackingLimits(maxUniformVectors, expandedUniforms);
gman@chromium.org8d804792012-10-17 21:33:48 +0000910}
911
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500912void TCompiler::initializeGLPosition(TIntermNode *root)
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800913{
Zhenyao Mo72111912016-07-20 17:45:56 -0700914 InitVariableList list;
915 sh::ShaderVariable var(GL_FLOAT_VEC4, 0);
916 var.name = "gl_Position";
917 list.push_back(var);
Zhenyao Mod7490962016-11-09 15:49:51 -0800918 InitializeVariables(root, list, symbolTable);
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800919}
920
Qin Jiajia7835b522016-10-08 11:20:17 +0800921void TCompiler::useAllMembersInUnusedStandardAndSharedBlocks(TIntermNode *root)
922{
923 sh::InterfaceBlockList list;
924
925 for (auto block : interfaceBlocks)
926 {
927 if (!block.staticUse &&
928 (block.layout == sh::BLOCKLAYOUT_STANDARD || block.layout == sh::BLOCKLAYOUT_SHARED))
929 {
930 list.push_back(block);
931 }
932 }
933
Zhenyao Mod7490962016-11-09 15:49:51 -0800934 sh::UseInterfaceBlockFields(root, list, symbolTable);
Qin Jiajia7835b522016-10-08 11:20:17 +0800935}
936
Olli Etuaho27776e32016-07-22 14:00:56 +0300937void TCompiler::initializeOutputVariables(TIntermNode *root)
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800938{
Zhenyao Mo72111912016-07-20 17:45:56 -0700939 InitVariableList list;
940 if (shaderType == GL_VERTEX_SHADER)
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800941 {
Zhenyao Mo72111912016-07-20 17:45:56 -0700942 for (auto var : varyings)
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800943 {
Zhenyao Mof9312682016-07-22 12:51:31 -0700944 list.push_back(var);
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800945 }
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800946 }
Zhenyao Mo72111912016-07-20 17:45:56 -0700947 else
948 {
949 ASSERT(shaderType == GL_FRAGMENT_SHADER);
950 for (auto var : outputVariables)
951 {
952 list.push_back(var);
953 }
954 }
Zhenyao Mod7490962016-11-09 15:49:51 -0800955 InitializeVariables(root, list, symbolTable);
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800956}
957
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500958const TExtensionBehavior &TCompiler::getExtensionBehavior() const
zmo@google.com5601ea02011-06-10 18:23:25 +0000959{
960 return extensionBehavior;
961}
zmo@google.com32e97312011-08-24 01:03:11 +0000962
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200963const char *TCompiler::getSourcePath() const
964{
965 return mSourcePath;
966}
967
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500968const ShBuiltInResources &TCompiler::getResources() const
shannon.woods%transgaming.com@gtempaccount.com18b4c4b2013-04-13 03:31:40 +0000969{
970 return compileResources;
971}
972
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500973const ArrayBoundsClamper &TCompiler::getArrayBoundsClamper() const
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000974{
975 return arrayBoundsClamper;
976}
977
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000978ShArrayIndexClampingStrategy TCompiler::getArrayIndexClampingStrategy() const
979{
980 return clampingStrategy;
981}
982
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500983const BuiltInFunctionEmulator &TCompiler::getBuiltInFunctionEmulator() const
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000984{
985 return builtInFunctionEmulator;
986}
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700987
Qiankun Miao7ebb97f2016-09-08 18:01:50 +0800988void TCompiler::writePragma(ShCompileOptions compileOptions)
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700989{
Kenneth Russellbccc65d2016-07-19 16:48:43 -0700990 if (!(compileOptions & SH_FLATTEN_PRAGMA_STDGL_INVARIANT_ALL))
991 {
992 TInfoSinkBase &sink = infoSink.obj;
993 if (mPragma.stdgl.invariantAll)
994 sink << "#pragma STDGL invariant(all)\n";
995 }
996}
997
998bool TCompiler::isVaryingDefined(const char *varyingName)
999{
1000 ASSERT(variablesCollected);
1001 for (size_t ii = 0; ii < varyings.size(); ++ii)
1002 {
1003 if (varyings[ii].name == varyingName)
1004 {
1005 return true;
1006 }
1007 }
1008
1009 return false;
Zhenyao Mo94ac7b72014-10-15 18:22:08 -07001010}
Jamie Madillacb4b812016-11-07 13:50:29 -05001011
1012} // namespace sh