blob: 9cedfab070db1f22124af7ecf8da48c8c4bde8c2 [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);
Andrei Volykhina5527072017-03-22 16:46:30 +0300574 // SamplerExternal2DY2YEXT is specified in the extension to have default precision.
575 initSamplerDefaultPrecision(EbtSamplerExternal2DY2YEXT);
Olli Etuaho183d7e22015-11-20 15:59:09 +0200576 // It isn't specified whether Sampler2DRect has default precision.
577 initSamplerDefaultPrecision(EbtSampler2DRect);
Nicolas Capens49a88872013-06-20 09:54:03 -0400578
Jamie Madill1b452142013-07-12 14:51:11 -0400579 InsertBuiltInFunctions(shaderType, shaderSpec, resources, symbolTable);
Nicolas Capens49a88872013-06-20 09:54:03 -0400580
581 IdentifyBuiltIns(shaderType, shaderSpec, resources, symbolTable);
582
583 return true;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000584}
585
Olli Etuaho183d7e22015-11-20 15:59:09 +0200586void TCompiler::initSamplerDefaultPrecision(TBasicType samplerType)
587{
588 ASSERT(samplerType > EbtGuardSamplerBegin && samplerType < EbtGuardSamplerEnd);
589 TPublicType sampler;
Martin Radev2cc85b32016-08-05 16:22:53 +0300590 sampler.initializeBasicType(samplerType);
Olli Etuaho183d7e22015-11-20 15:59:09 +0200591 symbolTable.setDefaultPrecision(sampler, EbpLow);
592}
593
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400594void TCompiler::setResourceString()
595{
596 std::ostringstream strstream;
Geoff Langb66a9092016-05-16 15:59:14 -0400597
598 // clang-format off
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400599 strstream << ":MaxVertexAttribs:" << compileResources.MaxVertexAttribs
Jamie Madillacb4b812016-11-07 13:50:29 -0500600 << ":MaxVertexUniformVectors:" << compileResources.MaxVertexUniformVectors
601 << ":MaxVaryingVectors:" << compileResources.MaxVaryingVectors
602 << ":MaxVertexTextureImageUnits:" << compileResources.MaxVertexTextureImageUnits
603 << ":MaxCombinedTextureImageUnits:" << compileResources.MaxCombinedTextureImageUnits
604 << ":MaxTextureImageUnits:" << compileResources.MaxTextureImageUnits
605 << ":MaxFragmentUniformVectors:" << compileResources.MaxFragmentUniformVectors
606 << ":MaxDrawBuffers:" << compileResources.MaxDrawBuffers
607 << ":OES_standard_derivatives:" << compileResources.OES_standard_derivatives
608 << ":OES_EGL_image_external:" << compileResources.OES_EGL_image_external
609 << ":OES_EGL_image_external_essl3:" << compileResources.OES_EGL_image_external_essl3
610 << ":NV_EGL_stream_consumer_external:" << compileResources.NV_EGL_stream_consumer_external
611 << ":ARB_texture_rectangle:" << compileResources.ARB_texture_rectangle
612 << ":EXT_draw_buffers:" << compileResources.EXT_draw_buffers
613 << ":FragmentPrecisionHigh:" << compileResources.FragmentPrecisionHigh
614 << ":MaxExpressionComplexity:" << compileResources.MaxExpressionComplexity
615 << ":MaxCallStackDepth:" << compileResources.MaxCallStackDepth
616 << ":MaxFunctionParameters:" << compileResources.MaxFunctionParameters
617 << ":EXT_blend_func_extended:" << compileResources.EXT_blend_func_extended
618 << ":EXT_frag_depth:" << compileResources.EXT_frag_depth
619 << ":EXT_shader_texture_lod:" << compileResources.EXT_shader_texture_lod
620 << ":EXT_shader_framebuffer_fetch:" << compileResources.EXT_shader_framebuffer_fetch
621 << ":NV_shader_framebuffer_fetch:" << compileResources.NV_shader_framebuffer_fetch
622 << ":ARM_shader_framebuffer_fetch:" << compileResources.ARM_shader_framebuffer_fetch
Andrei Volykhina5527072017-03-22 16:46:30 +0300623 << ":EXT_YUV_target:" << compileResources.EXT_YUV_target
Jamie Madillacb4b812016-11-07 13:50:29 -0500624 << ":MaxVertexOutputVectors:" << compileResources.MaxVertexOutputVectors
625 << ":MaxFragmentInputVectors:" << compileResources.MaxFragmentInputVectors
626 << ":MinProgramTexelOffset:" << compileResources.MinProgramTexelOffset
627 << ":MaxProgramTexelOffset:" << compileResources.MaxProgramTexelOffset
628 << ":MaxDualSourceDrawBuffers:" << compileResources.MaxDualSourceDrawBuffers
629 << ":NV_draw_buffers:" << compileResources.NV_draw_buffers
630 << ":WEBGL_debug_shader_precision:" << compileResources.WEBGL_debug_shader_precision
631 << ":MaxImageUnits:" << compileResources.MaxImageUnits
632 << ":MaxVertexImageUniforms:" << compileResources.MaxVertexImageUniforms
633 << ":MaxFragmentImageUniforms:" << compileResources.MaxFragmentImageUniforms
634 << ":MaxComputeImageUniforms:" << compileResources.MaxComputeImageUniforms
635 << ":MaxCombinedImageUniforms:" << compileResources.MaxCombinedImageUniforms
636 << ":MaxCombinedShaderOutputResources:" << compileResources.MaxCombinedShaderOutputResources
637 << ":MaxComputeWorkGroupCountX:" << compileResources.MaxComputeWorkGroupCount[0]
638 << ":MaxComputeWorkGroupCountY:" << compileResources.MaxComputeWorkGroupCount[1]
639 << ":MaxComputeWorkGroupCountZ:" << compileResources.MaxComputeWorkGroupCount[2]
640 << ":MaxComputeWorkGroupSizeX:" << compileResources.MaxComputeWorkGroupSize[0]
641 << ":MaxComputeWorkGroupSizeY:" << compileResources.MaxComputeWorkGroupSize[1]
642 << ":MaxComputeWorkGroupSizeZ:" << compileResources.MaxComputeWorkGroupSize[2]
643 << ":MaxComputeUniformComponents:" << compileResources.MaxComputeUniformComponents
644 << ":MaxComputeTextureImageUnits:" << compileResources.MaxComputeTextureImageUnits
645 << ":MaxComputeAtomicCounters:" << compileResources.MaxComputeAtomicCounters
646 << ":MaxComputeAtomicCounterBuffers:" << compileResources.MaxComputeAtomicCounterBuffers
647 << ":MaxVertexAtomicCounters:" << compileResources.MaxVertexAtomicCounters
648 << ":MaxFragmentAtomicCounters:" << compileResources.MaxFragmentAtomicCounters
649 << ":MaxCombinedAtomicCounters:" << compileResources.MaxCombinedAtomicCounters
650 << ":MaxAtomicCounterBindings:" << compileResources.MaxAtomicCounterBindings
651 << ":MaxVertexAtomicCounterBuffers:" << compileResources.MaxVertexAtomicCounterBuffers
652 << ":MaxFragmentAtomicCounterBuffers:" << compileResources.MaxFragmentAtomicCounterBuffers
653 << ":MaxCombinedAtomicCounterBuffers:" << compileResources.MaxCombinedAtomicCounterBuffers
654 << ":MaxAtomicCounterBufferSize:" << compileResources.MaxAtomicCounterBufferSize;
Geoff Langb66a9092016-05-16 15:59:14 -0400655 // clang-format on
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400656
657 builtInResourcesString = strstream.str();
658}
659
alokp@chromium.org07620a52010-09-23 17:53:56 +0000660void TCompiler::clearResults()
661{
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000662 arrayBoundsClamper.Cleanup();
alokp@chromium.org07620a52010-09-23 17:53:56 +0000663 infoSink.info.erase();
664 infoSink.obj.erase();
665 infoSink.debug.erase();
Olli Etuaho77ba4082016-12-16 12:01:18 +0000666 mDiagnostics.resetErrorCount();
alokp@chromium.org07620a52010-09-23 17:53:56 +0000667
Jamie Madilled27c722014-07-02 15:31:23 -0400668 attributes.clear();
669 outputVariables.clear();
alokp@chromium.org07620a52010-09-23 17:53:56 +0000670 uniforms.clear();
Jamie Madill23a8a432014-07-09 13:27:42 -0400671 expandedUniforms.clear();
Zhenyao Mod2d340b2013-09-23 14:57:05 -0400672 varyings.clear();
Jamie Madilled27c722014-07-02 15:31:23 -0400673 interfaceBlocks.clear();
Kenneth Russellbccc65d2016-07-19 16:48:43 -0700674 variablesCollected = false;
zmo@google.coma3b4ab42011-09-16 00:53:26 +0000675
Olli Etuaho09b04a22016-12-15 13:30:26 +0000676 mNumViews = -1;
677
Olli Etuahodfa75e82017-01-23 09:43:06 -0800678 builtInFunctionEmulator.cleanup();
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +0000679
680 nameMap.clear();
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200681
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500682 mSourcePath = NULL;
Corentin Wallezd4b50542015-09-28 12:19:26 -0700683 mTemporaryIndex = 0;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000684}
685
Corentin Wallez71d147f2015-02-11 11:15:24 -0800686bool TCompiler::initCallDag(TIntermNode *root)
zmo@google.comb1762df2011-07-30 02:04:23 +0000687{
Corentin Wallez71d147f2015-02-11 11:15:24 -0800688 mCallDag.clear();
689
Olli Etuaho77ba4082016-12-16 12:01:18 +0000690 switch (mCallDag.init(root, &mDiagnostics))
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800691 {
Jamie Madillacb4b812016-11-07 13:50:29 -0500692 case CallDAG::INITDAG_SUCCESS:
693 return true;
694 case CallDAG::INITDAG_RECURSION:
Jamie Madillacb4b812016-11-07 13:50:29 -0500695 case CallDAG::INITDAG_UNDEFINED:
Olli Etuaho77ba4082016-12-16 12:01:18 +0000696 // Error message has already been written out.
697 ASSERT(mDiagnostics.numErrors() > 0);
Jamie Madillacb4b812016-11-07 13:50:29 -0500698 return false;
Corentin Wallez71d147f2015-02-11 11:15:24 -0800699 }
700
701 UNREACHABLE();
702 return true;
703}
704
705bool TCompiler::checkCallDepth()
706{
707 std::vector<int> depths(mCallDag.size());
708
709 for (size_t i = 0; i < mCallDag.size(); i++)
710 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500711 int depth = 0;
Corentin Wallez71d147f2015-02-11 11:15:24 -0800712 auto &record = mCallDag.getRecordFromIndex(i);
713
714 for (auto &calleeIndex : record.callees)
715 {
716 depth = std::max(depth, depths[calleeIndex] + 1);
717 }
718
719 depths[i] = depth;
720
721 if (depth >= maxCallStackDepth)
722 {
723 // Trace back the function chain to have a meaningful info log.
Olli Etuaho77ba4082016-12-16 12:01:18 +0000724 std::stringstream errorStream;
725 errorStream << "Call stack too deep (larger than " << maxCallStackDepth
726 << ") with the following call chain: " << record.name;
Corentin Wallez71d147f2015-02-11 11:15:24 -0800727
Cooper Partin4d61f7e2015-08-12 10:56:50 -0700728 int currentFunction = static_cast<int>(i);
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500729 int currentDepth = depth;
Corentin Wallez71d147f2015-02-11 11:15:24 -0800730
731 while (currentFunction != -1)
732 {
Olli Etuaho77ba4082016-12-16 12:01:18 +0000733 errorStream << " -> " << mCallDag.getRecordFromIndex(currentFunction).name;
Corentin Wallez71d147f2015-02-11 11:15:24 -0800734
735 int nextFunction = -1;
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500736 for (auto &calleeIndex : mCallDag.getRecordFromIndex(currentFunction).callees)
Corentin Wallez71d147f2015-02-11 11:15:24 -0800737 {
738 if (depths[calleeIndex] == currentDepth - 1)
739 {
740 currentDepth--;
741 nextFunction = calleeIndex;
742 }
743 }
744
745 currentFunction = nextFunction;
746 }
747
Olli Etuaho77ba4082016-12-16 12:01:18 +0000748 std::string errorStr = errorStream.str();
749 mDiagnostics.globalError(errorStr.c_str());
750
Corentin Wallez71d147f2015-02-11 11:15:24 -0800751 return false;
752 }
753 }
754
755 return true;
756}
757
758bool TCompiler::tagUsedFunctions()
759{
760 // Search from main, starting from the end of the DAG as it usually is the root.
Cooper Partin4d61f7e2015-08-12 10:56:50 -0700761 for (size_t i = mCallDag.size(); i-- > 0;)
Corentin Wallez71d147f2015-02-11 11:15:24 -0800762 {
Olli Etuahoec9232b2017-03-27 17:01:37 +0300763 if (mCallDag.getRecordFromIndex(i).name == "main")
Corentin Wallez71d147f2015-02-11 11:15:24 -0800764 {
765 internalTagUsedFunction(i);
766 return true;
767 }
768 }
769
Olli Etuaho77ba4082016-12-16 12:01:18 +0000770 mDiagnostics.globalError("Missing main()");
Corentin Wallez71d147f2015-02-11 11:15:24 -0800771 return false;
772}
773
774void TCompiler::internalTagUsedFunction(size_t index)
775{
776 if (functionMetadata[index].used)
777 {
778 return;
779 }
780
781 functionMetadata[index].used = true;
782
783 for (int calleeIndex : mCallDag.getRecordFromIndex(index).callees)
784 {
785 internalTagUsedFunction(calleeIndex);
zmo@google.comb1762df2011-07-30 02:04:23 +0000786 }
787}
788
Corentin Walleza094a8a2015-04-07 11:53:06 -0700789// A predicate for the stl that returns if a top-level node is unused
790class TCompiler::UnusedPredicate
791{
792 public:
793 UnusedPredicate(const CallDAG *callDag, const std::vector<FunctionMetadata> *metadatas)
Jamie Madillacb4b812016-11-07 13:50:29 -0500794 : mCallDag(callDag), mMetadatas(metadatas)
Corentin Walleza094a8a2015-04-07 11:53:06 -0700795 {
796 }
797
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500798 bool operator()(TIntermNode *node)
Corentin Walleza094a8a2015-04-07 11:53:06 -0700799 {
Olli Etuaho16c745a2017-01-16 17:02:27 +0000800 const TIntermFunctionPrototype *asFunctionPrototype = node->getAsFunctionPrototypeNode();
801 const TIntermFunctionDefinition *asFunctionDefinition = node->getAsFunctionDefinition();
Corentin Walleza094a8a2015-04-07 11:53:06 -0700802
Olli Etuaho336b1472016-10-05 16:37:55 +0100803 const TFunctionSymbolInfo *functionInfo = nullptr;
804
Olli Etuaho16c745a2017-01-16 17:02:27 +0000805 if (asFunctionDefinition)
Olli Etuaho336b1472016-10-05 16:37:55 +0100806 {
Olli Etuaho16c745a2017-01-16 17:02:27 +0000807 functionInfo = asFunctionDefinition->getFunctionSymbolInfo();
Olli Etuaho336b1472016-10-05 16:37:55 +0100808 }
Olli Etuaho16c745a2017-01-16 17:02:27 +0000809 else if (asFunctionPrototype)
Olli Etuaho336b1472016-10-05 16:37:55 +0100810 {
Olli Etuaho16c745a2017-01-16 17:02:27 +0000811 functionInfo = asFunctionPrototype->getFunctionSymbolInfo();
Olli Etuaho336b1472016-10-05 16:37:55 +0100812 }
813 if (functionInfo == nullptr)
Corentin Walleza094a8a2015-04-07 11:53:06 -0700814 {
815 return false;
816 }
817
Olli Etuaho336b1472016-10-05 16:37:55 +0100818 size_t callDagIndex = mCallDag->findIndex(functionInfo);
Corentin Walleza094a8a2015-04-07 11:53:06 -0700819 if (callDagIndex == CallDAG::InvalidIndex)
820 {
821 // This happens only for unimplemented prototypes which are thus unused
Olli Etuaho16c745a2017-01-16 17:02:27 +0000822 ASSERT(asFunctionPrototype);
Corentin Walleza094a8a2015-04-07 11:53:06 -0700823 return true;
824 }
825
826 ASSERT(callDagIndex < mMetadatas->size());
827 return !(*mMetadatas)[callDagIndex].used;
828 }
829
830 private:
831 const CallDAG *mCallDag;
832 const std::vector<FunctionMetadata> *mMetadatas;
833};
834
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100835bool TCompiler::pruneUnusedFunctions(TIntermBlock *root)
Corentin Walleza094a8a2015-04-07 11:53:06 -0700836{
Corentin Walleza094a8a2015-04-07 11:53:06 -0700837 UnusedPredicate isUnused(&mCallDag, &functionMetadata);
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100838 TIntermSequence *sequence = root->getSequence();
Corentin Wallezb081e782015-07-20 05:40:04 -0700839
840 if (!sequence->empty())
841 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500842 sequence->erase(std::remove_if(sequence->begin(), sequence->end(), isUnused),
843 sequence->end());
Corentin Wallezb081e782015-07-20 05:40:04 -0700844 }
Corentin Walleza094a8a2015-04-07 11:53:06 -0700845
846 return true;
847}
848
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500849bool TCompiler::validateOutputs(TIntermNode *root)
Jamie Madill05a80ce2013-06-20 11:55:49 -0400850{
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +0300851 ValidateOutputs validateOutputs(getExtensionBehavior(), compileResources.MaxDrawBuffers);
Jamie Madill05a80ce2013-06-20 11:55:49 -0400852 root->traverse(&validateOutputs);
Olli Etuaho77ba4082016-12-16 12:01:18 +0000853 validateOutputs.validate(&mDiagnostics);
854 return (mDiagnostics.numErrors() == 0);
Jamie Madill05a80ce2013-06-20 11:55:49 -0400855}
856
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500857bool TCompiler::validateLimitations(TIntermNode *root)
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800858{
Olli Etuaho77ba4082016-12-16 12:01:18 +0000859 ValidateLimitations validate(shaderType, &mDiagnostics);
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000860 root->traverse(&validate);
Olli Etuaho77ba4082016-12-16 12:01:18 +0000861 return mDiagnostics.numErrors() == 0;
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000862}
863
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500864bool TCompiler::limitExpressionComplexity(TIntermNode *root)
Jamie Madilleb1a0102013-07-08 13:31:38 -0400865{
Jamie Madillacb4b812016-11-07 13:50:29 -0500866 TMaxDepthTraverser traverser(maxExpressionComplexity + 1);
Jamie Madilleb1a0102013-07-08 13:31:38 -0400867 root->traverse(&traverser);
Jamie Madill6654bc92014-03-26 14:01:57 -0400868
869 if (traverser.getMaxDepth() > maxExpressionComplexity)
870 {
Olli Etuaho77ba4082016-12-16 12:01:18 +0000871 mDiagnostics.globalError("Expression too complex.");
Jamie Madill6654bc92014-03-26 14:01:57 -0400872 return false;
873 }
874
Olli Etuaho19d1dc92016-03-08 17:18:46 +0200875 if (!ValidateMaxParameters::validate(root, maxFunctionParameters))
876 {
Olli Etuaho77ba4082016-12-16 12:01:18 +0000877 mDiagnostics.globalError("Function has too many parameters.");
Olli Etuaho19d1dc92016-03-08 17:18:46 +0200878 return false;
879 }
880
Jamie Madilleb1a0102013-07-08 13:31:38 -0400881 return true;
882}
883
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500884void TCompiler::collectVariables(TIntermNode *root)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000885{
Kenneth Russellbccc65d2016-07-19 16:48:43 -0700886 if (!variablesCollected)
887 {
888 sh::CollectVariables collect(&attributes, &outputVariables, &uniforms, &varyings,
Jamie Madillacb4b812016-11-07 13:50:29 -0500889 &interfaceBlocks, hashFunction, symbolTable,
890 extensionBehavior);
Kenneth Russellbccc65d2016-07-19 16:48:43 -0700891 root->traverse(&collect);
Jamie Madill23a8a432014-07-09 13:27:42 -0400892
Kenneth Russellbccc65d2016-07-19 16:48:43 -0700893 // This is for enforcePackingRestriction().
894 sh::ExpandUniforms(uniforms, &expandedUniforms);
895 variablesCollected = true;
896 }
alokp@chromium.org07620a52010-09-23 17:53:56 +0000897}
zmo@google.comfd747b82011-04-23 01:30:07 +0000898
Corentin Wallez1df16022016-10-27 08:16:56 -0400899bool TCompiler::shouldCollectVariables(ShCompileOptions compileOptions)
900{
901 return (compileOptions & SH_VARIABLES) != 0;
902}
903
904bool TCompiler::wereVariablesCollected() const
905{
906 return variablesCollected;
907}
908
gman@chromium.org8d804792012-10-17 21:33:48 +0000909bool TCompiler::enforcePackingRestrictions()
910{
911 VariablePacker packer;
Jamie Madill23a8a432014-07-09 13:27:42 -0400912 return packer.CheckVariablesWithinPackingLimits(maxUniformVectors, expandedUniforms);
gman@chromium.org8d804792012-10-17 21:33:48 +0000913}
914
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500915void TCompiler::initializeGLPosition(TIntermNode *root)
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800916{
Zhenyao Mo72111912016-07-20 17:45:56 -0700917 InitVariableList list;
918 sh::ShaderVariable var(GL_FLOAT_VEC4, 0);
919 var.name = "gl_Position";
920 list.push_back(var);
Zhenyao Mod7490962016-11-09 15:49:51 -0800921 InitializeVariables(root, list, symbolTable);
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800922}
923
Qin Jiajia7835b522016-10-08 11:20:17 +0800924void TCompiler::useAllMembersInUnusedStandardAndSharedBlocks(TIntermNode *root)
925{
926 sh::InterfaceBlockList list;
927
928 for (auto block : interfaceBlocks)
929 {
930 if (!block.staticUse &&
931 (block.layout == sh::BLOCKLAYOUT_STANDARD || block.layout == sh::BLOCKLAYOUT_SHARED))
932 {
933 list.push_back(block);
934 }
935 }
936
Zhenyao Mod7490962016-11-09 15:49:51 -0800937 sh::UseInterfaceBlockFields(root, list, symbolTable);
Qin Jiajia7835b522016-10-08 11:20:17 +0800938}
939
Olli Etuaho27776e32016-07-22 14:00:56 +0300940void TCompiler::initializeOutputVariables(TIntermNode *root)
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800941{
Zhenyao Mo72111912016-07-20 17:45:56 -0700942 InitVariableList list;
943 if (shaderType == GL_VERTEX_SHADER)
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800944 {
Zhenyao Mo72111912016-07-20 17:45:56 -0700945 for (auto var : varyings)
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800946 {
Zhenyao Mof9312682016-07-22 12:51:31 -0700947 list.push_back(var);
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800948 }
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800949 }
Zhenyao Mo72111912016-07-20 17:45:56 -0700950 else
951 {
952 ASSERT(shaderType == GL_FRAGMENT_SHADER);
953 for (auto var : outputVariables)
954 {
955 list.push_back(var);
956 }
957 }
Zhenyao Mod7490962016-11-09 15:49:51 -0800958 InitializeVariables(root, list, symbolTable);
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800959}
960
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500961const TExtensionBehavior &TCompiler::getExtensionBehavior() const
zmo@google.com5601ea02011-06-10 18:23:25 +0000962{
963 return extensionBehavior;
964}
zmo@google.com32e97312011-08-24 01:03:11 +0000965
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200966const char *TCompiler::getSourcePath() const
967{
968 return mSourcePath;
969}
970
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500971const ShBuiltInResources &TCompiler::getResources() const
shannon.woods%transgaming.com@gtempaccount.com18b4c4b2013-04-13 03:31:40 +0000972{
973 return compileResources;
974}
975
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500976const ArrayBoundsClamper &TCompiler::getArrayBoundsClamper() const
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000977{
978 return arrayBoundsClamper;
979}
980
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000981ShArrayIndexClampingStrategy TCompiler::getArrayIndexClampingStrategy() const
982{
983 return clampingStrategy;
984}
985
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500986const BuiltInFunctionEmulator &TCompiler::getBuiltInFunctionEmulator() const
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000987{
988 return builtInFunctionEmulator;
989}
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700990
Qiankun Miao7ebb97f2016-09-08 18:01:50 +0800991void TCompiler::writePragma(ShCompileOptions compileOptions)
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700992{
Kenneth Russellbccc65d2016-07-19 16:48:43 -0700993 if (!(compileOptions & SH_FLATTEN_PRAGMA_STDGL_INVARIANT_ALL))
994 {
995 TInfoSinkBase &sink = infoSink.obj;
996 if (mPragma.stdgl.invariantAll)
997 sink << "#pragma STDGL invariant(all)\n";
998 }
999}
1000
1001bool TCompiler::isVaryingDefined(const char *varyingName)
1002{
1003 ASSERT(variablesCollected);
1004 for (size_t ii = 0; ii < varyings.size(); ++ii)
1005 {
1006 if (varyings[ii].name == varyingName)
1007 {
1008 return true;
1009 }
1010 }
1011
1012 return false;
Zhenyao Mo94ac7b72014-10-15 18:22:08 -07001013}
Jamie Madillacb4b812016-11-07 13:50:29 -05001014
1015} // namespace sh