blob: eebc7c8fb910f2af6063ce044fb919519ccd5980 [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"
Martin Radev69056a12017-05-18 11:14:50 +030016#include "compiler/translator/DeclareAndInitBuiltinsForInstancedMultiview.h"
Olli Etuaho3d932d82016-04-12 11:10:30 +030017#include "compiler/translator/DeferGlobalInitializers.h"
Zhenyao Mo4e94fea2016-08-09 14:31:37 -070018#include "compiler/translator/EmulateGLFragColorBroadcast.h"
Jamie Madilld5696192016-10-06 11:09:24 -040019#include "compiler/translator/EmulatePrecision.h"
Geoff Lang17732822013-08-29 13:46:49 -040020#include "compiler/translator/Initialize.h"
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080021#include "compiler/translator/InitializeVariables.h"
Olli Etuaho9733cee2017-05-11 19:14:35 +030022#include "compiler/translator/IntermNodePatternMatcher.h"
Jamie Madill6b9cb252013-10-17 10:45:47 -040023#include "compiler/translator/ParseContext.h"
Olli Etuahoc6833112015-04-22 15:15:54 +030024#include "compiler/translator/PruneEmptyDeclarations.h"
Zhenyao Moe740add2014-07-18 17:01:01 -070025#include "compiler/translator/RegenerateStructNames.h"
Qiankun Miao705a9192016-08-29 10:05:27 +080026#include "compiler/translator/RemoveInvariantDeclaration.h"
Olli Etuaho5c407bb2015-06-01 12:20:39 +030027#include "compiler/translator/RemovePow.h"
Corentin Wallezd4b50542015-09-28 12:19:26 -070028#include "compiler/translator/RewriteDoWhile.h"
Zhenyao Mocd68fe72014-07-11 10:45:44 -070029#include "compiler/translator/ScalarizeVecAndMatConstructorArgs.h"
Olli Etuaho9733cee2017-05-11 19:14:35 +030030#include "compiler/translator/SeparateDeclarations.h"
31#include "compiler/translator/SimplifyLoopConditions.h"
Zhenyao Mo7cab38b2013-10-15 12:59:30 -070032#include "compiler/translator/UnfoldShortCircuitAST.h"
Qin Jiajia7835b522016-10-08 11:20:17 +080033#include "compiler/translator/UseInterfaceBlockFields.h"
Geoff Lang17732822013-08-29 13:46:49 -040034#include "compiler/translator/ValidateLimitations.h"
Olli Etuaho19d1dc92016-03-08 17:18:46 +020035#include "compiler/translator/ValidateMaxParameters.h"
Olli Etuaho09b04a22016-12-15 13:30:26 +000036#include "compiler/translator/ValidateMultiviewWebGL.h"
Geoff Lang17732822013-08-29 13:46:49 -040037#include "compiler/translator/ValidateOutputs.h"
38#include "compiler/translator/VariablePacker.h"
shannon.woods@transgaming.comda1ed362013-01-25 21:54:57 +000039#include "third_party/compiler/ArrayBoundsClamper.h"
Corentin Wallez28b65282016-06-16 07:24:50 -070040
Jamie Madillacb4b812016-11-07 13:50:29 -050041namespace sh
42{
43
Corentin Wallez28b65282016-06-16 07:24:50 -070044namespace
45{
46
47#if defined(ANGLE_ENABLE_FUZZER_CORPUS_OUTPUT)
48void DumpFuzzerCase(char const *const *shaderStrings,
49 size_t numStrings,
50 uint32_t type,
51 uint32_t spec,
52 uint32_t output,
53 uint64_t options)
54{
55 static int fileIndex = 0;
56
57 std::ostringstream o;
58 o << "corpus/" << fileIndex++ << ".sample";
59 std::string s = o.str();
60
61 // Must match the input format of the fuzzer
62 FILE *f = fopen(s.c_str(), "w");
63 fwrite(&type, sizeof(type), 1, f);
64 fwrite(&spec, sizeof(spec), 1, f);
65 fwrite(&output, sizeof(output), 1, f);
66 fwrite(&options, sizeof(options), 1, f);
67
68 char zero[128 - 20] = {0};
69 fwrite(&zero, 128 - 20, 1, f);
70
71 for (size_t i = 0; i < numStrings; i++)
72 {
73 fwrite(shaderStrings[i], sizeof(char), strlen(shaderStrings[i]), f);
74 }
75 fwrite(&zero, 1, 1, f);
76
77 fclose(f);
78}
79#endif // defined(ANGLE_ENABLE_FUZZER_CORPUS_OUTPUT)
80} // anonymous namespace
81
Jamie Madill5508f392014-02-20 13:31:36 -050082bool IsWebGLBasedSpec(ShShaderSpec spec)
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +000083{
Qiankun Miaoc2c5fc42016-08-31 15:24:22 +080084 return (spec == SH_WEBGL_SPEC || spec == SH_WEBGL2_SPEC || spec == SH_WEBGL3_SPEC);
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +000085}
86
Qingqing Dengad0d0792015-04-08 14:25:06 -070087bool IsGLSL130OrNewer(ShShaderOutput output)
88{
Jamie Madillacb4b812016-11-07 13:50:29 -050089 return (output == SH_GLSL_130_OUTPUT || output == SH_GLSL_140_OUTPUT ||
90 output == SH_GLSL_150_CORE_OUTPUT || output == SH_GLSL_330_CORE_OUTPUT ||
91 output == SH_GLSL_400_CORE_OUTPUT || output == SH_GLSL_410_CORE_OUTPUT ||
92 output == SH_GLSL_420_CORE_OUTPUT || output == SH_GLSL_430_CORE_OUTPUT ||
93 output == SH_GLSL_440_CORE_OUTPUT || output == SH_GLSL_450_CORE_OUTPUT);
Qingqing Dengad0d0792015-04-08 14:25:06 -070094}
95
Qiankun Miao705a9192016-08-29 10:05:27 +080096bool IsGLSL420OrNewer(ShShaderOutput output)
97{
Jamie Madillacb4b812016-11-07 13:50:29 -050098 return (output == SH_GLSL_420_CORE_OUTPUT || output == SH_GLSL_430_CORE_OUTPUT ||
99 output == SH_GLSL_440_CORE_OUTPUT || output == SH_GLSL_450_CORE_OUTPUT);
Qiankun Miao705a9192016-08-29 10:05:27 +0800100}
101
Zhenyao Mob7bf7422016-11-08 14:44:05 -0800102bool IsGLSL410OrOlder(ShShaderOutput output)
103{
104 return (output == SH_GLSL_130_OUTPUT || output == SH_GLSL_140_OUTPUT ||
105 output == SH_GLSL_150_CORE_OUTPUT || output == SH_GLSL_330_CORE_OUTPUT ||
106 output == SH_GLSL_400_CORE_OUTPUT || output == SH_GLSL_410_CORE_OUTPUT);
107}
108
Qiankun Miao89dd8f32016-11-09 12:59:30 +0000109bool RemoveInvariant(sh::GLenum shaderType,
110 int shaderVersion,
111 ShShaderOutput outputType,
112 ShCompileOptions compileOptions)
113{
114 if ((compileOptions & SH_DONT_REMOVE_INVARIANT_FOR_FRAGMENT_INPUT) == 0 &&
115 shaderType == GL_FRAGMENT_SHADER && IsGLSL420OrNewer(outputType))
116 return true;
117
118 if ((compileOptions & SH_REMOVE_INVARIANT_AND_CENTROID_FOR_ESSL3) != 0 &&
Qiankun Miao41f9f672016-11-16 17:04:36 +0800119 shaderVersion >= 300 && shaderType == GL_VERTEX_SHADER)
Qiankun Miao89dd8f32016-11-09 12:59:30 +0000120 return true;
121
122 return false;
123}
124
Zhenyao Mo7faf1a12014-04-25 18:03:56 -0700125size_t GetGlobalMaxTokenSize(ShShaderSpec spec)
Jamie Madill88f6e942014-02-19 10:27:53 -0500126{
He Yunchao29ab9ff2015-08-06 16:58:30 +0800127 // WebGL defines a max token length of 256, while ES2 leaves max token
Jamie Madill88f6e942014-02-19 10:27:53 -0500128 // size undefined. ES3 defines a max size of 1024 characters.
Zhenyao Modb9b40b2014-10-29 15:00:04 -0700129 switch (spec)
Jamie Madill88f6e942014-02-19 10:27:53 -0500130 {
Jamie Madillacb4b812016-11-07 13:50:29 -0500131 case SH_WEBGL_SPEC:
132 return 256;
133 default:
134 return 1024;
Jamie Madill88f6e942014-02-19 10:27:53 -0500135 }
136}
137
Shao77891c02017-06-23 16:30:17 +0800138int GetMaxUniformVectorsForShaderType(GLenum shaderType, const ShBuiltInResources &resources)
139{
140 switch (shaderType)
141 {
142 case GL_VERTEX_SHADER:
143 return resources.MaxVertexUniformVectors;
144 case GL_FRAGMENT_SHADER:
145 return resources.MaxFragmentUniformVectors;
146 case GL_COMPUTE_SHADER:
147 // TODO (jiawei.shao@intel.com): check if we need finer-grained component counting
148 return resources.MaxComputeUniformComponents / 4;
149 default:
150 UNIMPLEMENTED();
151 return -1;
152 }
153}
154
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500155namespace
156{
Zhenyao Modb9b40b2014-10-29 15:00:04 -0700157
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800158class TScopedPoolAllocator
159{
160 public:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500161 TScopedPoolAllocator(TPoolAllocator *allocator) : mAllocator(allocator)
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800162 {
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400163 mAllocator->push();
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000164 SetGlobalPoolAllocator(mAllocator);
165 }
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800166 ~TScopedPoolAllocator()
167 {
Yunchao Hef81ce4a2017-04-24 10:49:17 +0800168 SetGlobalPoolAllocator(nullptr);
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400169 mAllocator->pop();
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000170 }
171
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800172 private:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500173 TPoolAllocator *mAllocator;
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400174};
175
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800176class TScopedSymbolTableLevel
177{
178 public:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500179 TScopedSymbolTableLevel(TSymbolTable *table) : mTable(table)
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800180 {
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400181 ASSERT(mTable->atBuiltInLevel());
182 mTable->push();
183 }
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800184 ~TScopedSymbolTableLevel()
185 {
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400186 while (!mTable->atBuiltInLevel())
187 mTable->pop();
188 }
189
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800190 private:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500191 TSymbolTable *mTable;
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000192};
Zhenyao Modb9b40b2014-10-29 15:00:04 -0700193
194int MapSpecToShaderVersion(ShShaderSpec spec)
195{
196 switch (spec)
197 {
Jamie Madillacb4b812016-11-07 13:50:29 -0500198 case SH_GLES2_SPEC:
199 case SH_WEBGL_SPEC:
200 return 100;
201 case SH_GLES3_SPEC:
202 case SH_WEBGL2_SPEC:
203 return 300;
204 case SH_GLES3_1_SPEC:
205 case SH_WEBGL3_SPEC:
206 return 310;
207 default:
208 UNREACHABLE();
209 return 0;
Zhenyao Modb9b40b2014-10-29 15:00:04 -0700210 }
211}
212
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000213} // namespace
214
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800215TShHandleBase::TShHandleBase()
216{
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000217 allocator.push();
218 SetGlobalPoolAllocator(&allocator);
219}
220
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800221TShHandleBase::~TShHandleBase()
222{
Yunchao Hef81ce4a2017-04-24 10:49:17 +0800223 SetGlobalPoolAllocator(nullptr);
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000224 allocator.popAll();
225}
226
Jamie Madill183bde52014-07-02 15:31:19 -0400227TCompiler::TCompiler(sh::GLenum type, ShShaderSpec spec, ShShaderOutput output)
Kenneth Russellbccc65d2016-07-19 16:48:43 -0700228 : variablesCollected(false),
Olli Etuahob12040c2017-06-27 14:20:45 +0300229 mGLPositionInitialized(false),
Kenneth Russellbccc65d2016-07-19 16:48:43 -0700230 shaderType(type),
zmo@google.comf420c422011-09-12 18:27:59 +0000231 shaderSpec(spec),
Jamie Madill68fe74a2014-05-27 12:56:01 -0400232 outputType(output),
Jamie Madilleb1a0102013-07-08 13:31:38 -0400233 maxUniformVectors(0),
234 maxExpressionComplexity(0),
235 maxCallStackDepth(0),
Olli Etuaho19d1dc92016-03-08 17:18:46 +0200236 maxFunctionParameters(0),
shannon.woods%transgaming.com@gtempaccount.comcbb6b6a2013-04-13 03:27:47 +0000237 fragmentPrecisionHigh(false),
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000238 clampingStrategy(SH_CLAMP_WITH_CLAMP_INTRINSIC),
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200239 builtInFunctionEmulator(),
Olli Etuaho77ba4082016-12-16 12:01:18 +0000240 mDiagnostics(infoSink.info),
Yunchao Hef81ce4a2017-04-24 10:49:17 +0800241 mSourcePath(nullptr),
Martin Radev802abe02016-08-04 17:48:32 +0300242 mComputeShaderLocalSizeDeclared(false),
Olli Etuaho4dd06d52017-07-05 12:41:06 +0300243 mTemporaryId()
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000244{
Martin Radev802abe02016-08-04 17:48:32 +0300245 mComputeShaderLocalSize.fill(1);
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000246}
247
248TCompiler::~TCompiler()
249{
250}
251
Qiankun Miao7ebb97f2016-09-08 18:01:50 +0800252bool TCompiler::shouldRunLoopAndIndexingValidation(ShCompileOptions compileOptions) const
Olli Etuaho5d91dda2015-06-18 15:47:46 +0300253{
254 // If compiling an ESSL 1.00 shader for WebGL, or if its been requested through the API,
255 // validate loop and indexing as well (to verify that the shader only uses minimal functionality
256 // of ESSL 1.00 as in Appendix A of the spec).
257 return (IsWebGLBasedSpec(shaderSpec) && shaderVersion == 100) ||
258 (compileOptions & SH_VALIDATE_LOOP_INDEXING);
259}
260
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500261bool TCompiler::Init(const ShBuiltInResources &resources)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000262{
Shao77891c02017-06-23 16:30:17 +0800263 shaderVersion = 100;
264
265 maxUniformVectors = GetMaxUniformVectorsForShaderType(shaderType, resources);
266
Jamie Madilleb1a0102013-07-08 13:31:38 -0400267 maxExpressionComplexity = resources.MaxExpressionComplexity;
Olli Etuaho19d1dc92016-03-08 17:18:46 +0200268 maxCallStackDepth = resources.MaxCallStackDepth;
269 maxFunctionParameters = resources.MaxFunctionParameters;
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400270
271 SetGlobalPoolAllocator(&allocator);
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000272
alokp@chromium.org07620a52010-09-23 17:53:56 +0000273 // Generate built-in symbol table.
274 if (!InitBuiltInSymbolTable(resources))
275 return false;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000276 InitExtensionBehavior(resources, extensionBehavior);
shannon.woods%transgaming.com@gtempaccount.comcbb6b6a2013-04-13 03:27:47 +0000277 fragmentPrecisionHigh = resources.FragmentPrecisionHigh == 1;
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000278
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000279 arrayBoundsClamper.SetClampingStrategy(resources.ArrayIndexClampingStrategy);
280 clampingStrategy = resources.ArrayIndexClampingStrategy;
281
daniel@transgaming.comc23f4612012-11-28 19:42:57 +0000282 hashFunction = resources.HashFunction;
283
alokp@chromium.org07620a52010-09-23 17:53:56 +0000284 return true;
285}
286
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100287TIntermBlock *TCompiler::compileTreeForTesting(const char *const shaderStrings[],
288 size_t numStrings,
289 ShCompileOptions compileOptions)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000290{
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200291 return compileTreeImpl(shaderStrings, numStrings, compileOptions);
292}
293
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100294TIntermBlock *TCompiler::compileTreeImpl(const char *const shaderStrings[],
295 size_t numStrings,
296 const ShCompileOptions compileOptions)
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200297{
alokp@chromium.org07620a52010-09-23 17:53:56 +0000298 clearResults();
299
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200300 ASSERT(numStrings > 0);
301 ASSERT(GetGlobalPoolAllocator());
alokp@chromium.org07620a52010-09-23 17:53:56 +0000302
David Yen0fbd1282015-02-02 14:46:09 -0800303 // Reset the extension behavior for each compilation unit.
304 ResetExtensionBehavior(extensionBehavior);
305
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000306 // First string is path of source file if flag is set. The actual source follows.
shannon.woods@transgaming.comd64b3da2013-02-28 23:19:26 +0000307 size_t firstSource = 0;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000308 if (compileOptions & SH_SOURCE_PATH)
309 {
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200310 mSourcePath = shaderStrings[0];
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000311 ++firstSource;
312 }
313
Olli Etuahof119a262016-08-19 15:54:22 +0300314 TParseContext parseContext(symbolTable, extensionBehavior, shaderType, shaderSpec,
Olli Etuaho77ba4082016-12-16 12:01:18 +0000315 compileOptions, true, &mDiagnostics, getResources());
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200316
Olli Etuahoa6996682015-10-12 14:32:30 +0300317 parseContext.setFragmentPrecisionHighOnESSL1(fragmentPrecisionHigh);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000318
319 // We preserve symbols at the built-in level from compile-to-compile.
320 // Start pushing the user-defined symbols at global level.
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400321 TScopedSymbolTableLevel scopedSymbolLevel(&symbolTable);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000322
323 // Parse shader.
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500324 bool success = (PaParseStrings(numStrings - firstSource, &shaderStrings[firstSource], nullptr,
325 &parseContext) == 0) &&
326 (parseContext.getTreeRoot() != nullptr);
shannon.woods%transgaming.com@gtempaccount.com0bbed382013-04-13 03:38:07 +0000327
shannon.woods%transgaming.com@gtempaccount.com5524db02013-04-13 03:38:16 +0000328 shaderVersion = parseContext.getShaderVersion();
Zhenyao Modb9b40b2014-10-29 15:00:04 -0700329 if (success && MapSpecToShaderVersion(shaderSpec) < shaderVersion)
330 {
Olli Etuaho77ba4082016-12-16 12:01:18 +0000331 mDiagnostics.globalError("unsupported shader version");
Zhenyao Modb9b40b2014-10-29 15:00:04 -0700332 success = false;
333 }
shannon.woods%transgaming.com@gtempaccount.com0bbed382013-04-13 03:38:07 +0000334
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100335 TIntermBlock *root = nullptr;
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200336
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800337 if (success)
338 {
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700339 mPragma = parseContext.pragma();
Kenneth Russell8bad46d2016-07-01 19:52:52 -0700340 symbolTable.setGlobalInvariant(mPragma.stdgl.invariantAll);
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700341
Martin Radev802abe02016-08-04 17:48:32 +0300342 mComputeShaderLocalSizeDeclared = parseContext.isComputeShaderLocalSizeDeclared();
343 mComputeShaderLocalSize = parseContext.getComputeShaderLocalSize();
344
Olli Etuaho09b04a22016-12-15 13:30:26 +0000345 mNumViews = parseContext.getNumViews();
346
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400347 root = parseContext.getTreeRoot();
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000348
Olli Etuahoa6996682015-10-12 14:32:30 +0300349 // Highp might have been auto-enabled based on shader version
350 fragmentPrecisionHigh = parseContext.getFragmentPrecisionHigh();
351
Olli Etuaho09b04a22016-12-15 13:30:26 +0000352 if (success && (IsWebGLBasedSpec(shaderSpec) &&
353 IsExtensionEnabled(extensionBehavior, "GL_OVR_multiview") &&
354 IsExtensionEnabled(extensionBehavior, "GL_OVR_multiview2")))
355 {
356 // Can't enable both extensions at the same time.
357 mDiagnostics.globalError("Can't enable both OVR_multiview and OVR_multiview2");
358 success = false;
359 }
360
Jamie Madill6654bc92014-03-26 14:01:57 -0400361 // Disallow expressions deemed too complex.
362 if (success && (compileOptions & SH_LIMIT_EXPRESSION_COMPLEXITY))
363 success = limitExpressionComplexity(root);
364
Corentin Wallez71d147f2015-02-11 11:15:24 -0800365 // Create the function DAG and check there is no recursion
zmo@google.comb1762df2011-07-30 02:04:23 +0000366 if (success)
Corentin Wallez71d147f2015-02-11 11:15:24 -0800367 success = initCallDag(root);
368
369 if (success && (compileOptions & SH_LIMIT_CALL_STACK_DEPTH))
370 success = checkCallDepth();
371
372 // Checks which functions are used and if "main" exists
373 if (success)
374 {
375 functionMetadata.clear();
376 functionMetadata.resize(mCallDag.size());
377 success = tagUsedFunctions();
378 }
zmo@google.comb1762df2011-07-30 02:04:23 +0000379
Corentin Walleza094a8a2015-04-07 11:53:06 -0700380 if (success && !(compileOptions & SH_DONT_PRUNE_UNUSED_FUNCTIONS))
381 success = pruneUnusedFunctions(root);
382
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500383 // Prune empty declarations to work around driver bugs and to keep declaration output
384 // simple.
Olli Etuahoc6833112015-04-22 15:15:54 +0300385 if (success)
386 PruneEmptyDeclarations(root);
387
Andrei Volykhin73fa4b82017-05-05 18:45:06 +0300388 if (success && shaderVersion >= 300 && shaderType == GL_FRAGMENT_SHADER)
Olli Etuaho12b0b392017-05-30 13:22:31 +0300389 {
390 success = ValidateOutputs(root, getExtensionBehavior(), compileResources.MaxDrawBuffers,
391 &mDiagnostics);
392 }
Jamie Madill05a80ce2013-06-20 11:55:49 -0400393
Olli Etuaho5d91dda2015-06-18 15:47:46 +0300394 if (success && shouldRunLoopAndIndexingValidation(compileOptions))
Olli Etuaho9ec79392017-03-31 23:04:23 +0300395 success =
396 ValidateLimitations(root, shaderType, symbolTable, shaderVersion, &mDiagnostics);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000397
Olli Etuaho09b04a22016-12-15 13:30:26 +0000398 bool multiview2 = IsExtensionEnabled(extensionBehavior, "GL_OVR_multiview2");
399 if (success && compileResources.OVR_multiview && IsWebGLBasedSpec(shaderSpec) &&
400 (IsExtensionEnabled(extensionBehavior, "GL_OVR_multiview") || multiview2))
Olli Etuaho01d0ad02017-01-22 14:51:23 -0800401 success = ValidateMultiviewWebGL(root, shaderType, symbolTable, shaderVersion,
402 multiview2, &mDiagnostics);
Olli Etuaho09b04a22016-12-15 13:30:26 +0000403
Jamie Madilld5696192016-10-06 11:09:24 -0400404 // Fail compilation if precision emulation not supported.
405 if (success && getResources().WEBGL_debug_shader_precision &&
406 getPragma().debugShaderPrecision)
407 {
408 if (!EmulatePrecision::SupportedInLanguage(outputType))
409 {
Olli Etuaho77ba4082016-12-16 12:01:18 +0000410 mDiagnostics.globalError("Precision emulation not supported for this output type.");
Jamie Madilld5696192016-10-06 11:09:24 -0400411 success = false;
412 }
413 }
414
zmo@google.com32e97312011-08-24 01:03:11 +0000415 // Built-in function emulation needs to happen after validateLimitations pass.
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200416 if (success)
417 {
Jamie Madill438dbcf2016-06-17 14:20:05 -0400418 // TODO(jmadill): Remove global pool allocator.
419 GetGlobalPoolAllocator()->lock();
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200420 initBuiltInFunctionEmulator(&builtInFunctionEmulator, compileOptions);
Jamie Madill438dbcf2016-06-17 14:20:05 -0400421 GetGlobalPoolAllocator()->unlock();
Olli Etuahodfa75e82017-01-23 09:43:06 -0800422 builtInFunctionEmulator.markBuiltInFunctionsForEmulation(root);
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200423 }
zmo@google.com32e97312011-08-24 01:03:11 +0000424
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000425 // Clamping uniform array bounds needs to happen after validateLimitations pass.
426 if (success && (compileOptions & SH_CLAMP_INDIRECT_ARRAY_BOUNDS))
427 arrayBoundsClamper.MarkIndirectArrayBoundsForClamping(root);
428
Martin Radev69056a12017-05-18 11:14:50 +0300429 if (success && (compileOptions & SH_INITIALIZE_BUILTINS_FOR_INSTANCED_MULTIVIEW) &&
430 parseContext.isMultiviewExtensionEnabled() && getShaderType() == GL_VERTEX_SHADER)
431 {
432 DeclareAndInitBuiltinsForInstancedMultiview(root, getNumViews());
433 }
434
Corentin Wallezd4b50542015-09-28 12:19:26 -0700435 // This pass might emit short circuits so keep it before the short circuit unfolding
436 if (success && (compileOptions & SH_REWRITE_DO_WHILE_LOOPS))
Olli Etuaho4dd06d52017-07-05 12:41:06 +0300437 RewriteDoWhile(root, getTemporaryId());
Corentin Wallezd4b50542015-09-28 12:19:26 -0700438
Qiankun Miao09cfac62016-09-06 17:25:16 +0800439 if (success && (compileOptions & SH_ADD_AND_TRUE_TO_LOOP_CONDITION))
440 sh::AddAndTrueToLoopCondition(root);
441
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800442 if (success && (compileOptions & SH_UNFOLD_SHORT_CIRCUIT))
443 {
Zhenyao Mo7cab38b2013-10-15 12:59:30 -0700444 UnfoldShortCircuitAST unfoldShortCircuit;
445 root->traverse(&unfoldShortCircuit);
446 unfoldShortCircuit.updateTree();
447 }
448
Olli Etuaho5c407bb2015-06-01 12:20:39 +0300449 if (success && (compileOptions & SH_REMOVE_POW_WITH_CONSTANT_EXPONENT))
450 {
451 RemovePow(root);
452 }
453
Olli Etuaho4dfe8092015-08-21 17:44:35 +0300454 if (success && shouldCollectVariables(compileOptions))
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800455 {
Olli Etuaho19515012017-06-26 18:00:17 +0300456 ASSERT(!variablesCollected);
457 CollectVariables(root, &attributes, &outputVariables, &uniforms, &varyings,
458 &interfaceBlocks, hashFunction, symbolTable, shaderVersion,
459 extensionBehavior);
460 variablesCollected = true;
Qin Jiajia7835b522016-10-08 11:20:17 +0800461 if (compileOptions & SH_USE_UNUSED_STANDARD_SHARED_BLOCKS)
462 {
463 useAllMembersInUnusedStandardAndSharedBlocks(root);
464 }
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800465 if (compileOptions & SH_ENFORCE_PACKING_RESTRICTIONS)
466 {
Olli Etuaho19515012017-06-26 18:00:17 +0300467 std::vector<sh::ShaderVariable> expandedUniforms;
468 sh::ExpandUniforms(uniforms, &expandedUniforms);
469 VariablePacker packer;
470 // Returns true if, after applying the packing rules in the GLSL ES 1.00.17 spec
471 // Appendix A, section 7, the shader does not use too many uniforms.
472 success =
473 packer.CheckVariablesWithinPackingLimits(maxUniformVectors, expandedUniforms);
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800474 if (!success)
475 {
Olli Etuaho77ba4082016-12-16 12:01:18 +0000476 mDiagnostics.globalError("too many uniforms");
gman@chromium.org8d804792012-10-17 21:33:48 +0000477 }
478 }
Zhenyao Mo72111912016-07-20 17:45:56 -0700479 if (success && (compileOptions & SH_INIT_OUTPUT_VARIABLES))
480 {
Olli Etuaho27776e32016-07-22 14:00:56 +0300481 initializeOutputVariables(root);
Zhenyao Mo72111912016-07-20 17:45:56 -0700482 }
gman@chromium.org8d804792012-10-17 21:33:48 +0000483 }
zmo@google.comfd747b82011-04-23 01:30:07 +0000484
Olli Etuahob12040c2017-06-27 14:20:45 +0300485 // gl_Position is always written in compatibility output mode.
486 // It may have been already initialized among other output variables, in that case we don't
487 // need to initialize it twice.
488 if (success && shaderType == GL_VERTEX_SHADER && !mGLPositionInitialized &&
489 ((compileOptions & SH_INIT_GL_POSITION) ||
490 (outputType == SH_GLSL_COMPATIBILITY_OUTPUT)))
491 {
492 initializeGLPosition(root);
493 mGLPositionInitialized = true;
494 }
495
Yuly Novikov817232e2017-02-22 18:36:10 -0500496 // Removing invariant declarations must be done after collecting variables.
497 // Otherwise, built-in invariant declarations don't apply.
498 if (success && RemoveInvariant(shaderType, shaderVersion, outputType, compileOptions))
499 sh::RemoveInvariantDeclaration(root);
500
Zhenyao Mocd68fe72014-07-11 10:45:44 -0700501 if (success && (compileOptions & SH_SCALARIZE_VEC_AND_MAT_CONSTRUCTOR_ARGS))
502 {
Olli Etuahob990b552016-10-27 12:29:17 +0100503 ScalarizeVecAndMatConstructorArgs(root, shaderType, fragmentPrecisionHigh,
Olli Etuaho4dd06d52017-07-05 12:41:06 +0300504 getTemporaryId());
Zhenyao Mocd68fe72014-07-11 10:45:44 -0700505 }
506
Zhenyao Moe740add2014-07-18 17:01:01 -0700507 if (success && (compileOptions & SH_REGENERATE_STRUCT_NAMES))
508 {
509 RegenerateStructNames gen(symbolTable, shaderVersion);
510 root->traverse(&gen);
511 }
Olli Etuaho3d932d82016-04-12 11:10:30 +0300512
Zhenyao Mo4e94fea2016-08-09 14:31:37 -0700513 if (success && shaderType == GL_FRAGMENT_SHADER && shaderVersion == 100 &&
514 compileResources.EXT_draw_buffers && compileResources.MaxDrawBuffers > 1 &&
515 IsExtensionEnabled(extensionBehavior, "GL_EXT_draw_buffers"))
516 {
517 EmulateGLFragColorBroadcast(root, compileResources.MaxDrawBuffers, &outputVariables);
518 }
519
Olli Etuaho3d932d82016-04-12 11:10:30 +0300520 if (success)
521 {
Olli Etuaho0ffc4412017-05-19 14:18:55 +0300522 DeferGlobalInitializers(root, needToInitializeGlobalsInAST());
Olli Etuaho3d932d82016-04-12 11:10:30 +0300523 }
Olli Etuaho9733cee2017-05-11 19:14:35 +0300524
525 if (success && (compileOptions & SH_INITIALIZE_UNINITIALIZED_LOCALS) && getOutputType())
526 {
527 // Initialize uninitialized local variables.
528 // In some cases initializing can generate extra statements in the parent block, such as
529 // when initializing nameless structs or initializing arrays in ESSL 1.00. In that case
530 // we need to first simplify loop conditions and separate declarations. If we don't
531 // follow the Appendix A limitations, loop init statements can declare arrays or
532 // nameless structs and have multiple declarations.
533
534 if (!shouldRunLoopAndIndexingValidation(compileOptions))
535 {
536 SimplifyLoopConditions(root,
537 IntermNodePatternMatcher::kMultiDeclaration |
538 IntermNodePatternMatcher::kArrayDeclaration |
539 IntermNodePatternMatcher::kNamelessStructDeclaration,
Olli Etuaho4dd06d52017-07-05 12:41:06 +0300540 getTemporaryId(), getSymbolTable(), getShaderVersion());
Olli Etuaho9733cee2017-05-11 19:14:35 +0300541 }
542 // We only really need to separate array declarations and nameless struct declarations,
543 // but it's simpler to just use the regular SeparateDeclarations.
544 SeparateDeclarations(root);
545 InitializeUninitializedLocals(root, getShaderVersion());
546 }
alokp@chromium.org07620a52010-09-23 17:53:56 +0000547 }
548
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200549 if (success)
550 return root;
551
Yunchao Hef81ce4a2017-04-24 10:49:17 +0800552 return nullptr;
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200553}
554
Qiankun Miao7ebb97f2016-09-08 18:01:50 +0800555bool TCompiler::compile(const char *const shaderStrings[],
556 size_t numStrings,
557 ShCompileOptions compileOptionsIn)
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200558{
Corentin Wallez28b65282016-06-16 07:24:50 -0700559#if defined(ANGLE_ENABLE_FUZZER_CORPUS_OUTPUT)
560 DumpFuzzerCase(shaderStrings, numStrings, shaderType, shaderSpec, outputType, compileOptionsIn);
561#endif // defined(ANGLE_ENABLE_FUZZER_CORPUS_OUTPUT)
562
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200563 if (numStrings == 0)
564 return true;
565
Qiankun Miao7ebb97f2016-09-08 18:01:50 +0800566 ShCompileOptions compileOptions = compileOptionsIn;
Kenneth Russellbccc65d2016-07-19 16:48:43 -0700567
568 // Apply key workarounds.
569 if (shouldFlattenPragmaStdglInvariantAll())
570 {
571 // This should be harmless to do in all cases, but for the moment, do it only conditionally.
572 compileOptions |= SH_FLATTEN_PRAGMA_STDGL_INVARIANT_ALL;
573 }
574
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200575 TScopedPoolAllocator scopedAlloc(&allocator);
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100576 TIntermBlock *root = compileTreeImpl(shaderStrings, numStrings, compileOptions);
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200577
578 if (root)
579 {
580 if (compileOptions & SH_INTERMEDIATE_TREE)
581 TIntermediate::outputTree(root, infoSink.info);
582
583 if (compileOptions & SH_OBJECT_CODE)
584 translate(root, compileOptions);
585
586 // The IntermNode tree doesn't need to be deleted here, since the
587 // memory will be freed in a big chunk by the PoolAllocator.
588 return true;
589 }
590 return false;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000591}
592
Nicolas Capens49a88872013-06-20 09:54:03 -0400593bool TCompiler::InitBuiltInSymbolTable(const ShBuiltInResources &resources)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000594{
Olli Etuaho28cb0362016-11-22 15:42:37 +0000595 if (resources.MaxDrawBuffers < 1)
596 {
597 return false;
598 }
599 if (resources.EXT_blend_func_extended && resources.MaxDualSourceDrawBuffers < 1)
600 {
601 return false;
602 }
603
shannon.woods%transgaming.com@gtempaccount.com18b4c4b2013-04-13 03:31:40 +0000604 compileResources = resources;
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400605 setResourceString();
shannonwoods@chromium.org2ac0be92013-05-30 00:02:27 +0000606
Nicolas Capens49a88872013-06-20 09:54:03 -0400607 assert(symbolTable.isEmpty());
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500608 symbolTable.push(); // COMMON_BUILTINS
609 symbolTable.push(); // ESSL1_BUILTINS
610 symbolTable.push(); // ESSL3_BUILTINS
611 symbolTable.push(); // ESSL3_1_BUILTINS
shannonwoods@chromium.org2ac0be92013-05-30 00:02:27 +0000612
Jamie Madillacb4b812016-11-07 13:50:29 -0500613 switch (shaderType)
Nicolas Capens49a88872013-06-20 09:54:03 -0400614 {
Jamie Madillacb4b812016-11-07 13:50:29 -0500615 case GL_FRAGMENT_SHADER:
Olli Etuahocce89652017-06-19 16:04:09 +0300616 symbolTable.setDefaultPrecision(EbtInt, EbpMedium);
Jamie Madillacb4b812016-11-07 13:50:29 -0500617 break;
618 case GL_VERTEX_SHADER:
Olli Etuahocce89652017-06-19 16:04:09 +0300619 symbolTable.setDefaultPrecision(EbtInt, EbpHigh);
620 symbolTable.setDefaultPrecision(EbtFloat, EbpHigh);
Jamie Madillacb4b812016-11-07 13:50:29 -0500621 break;
622 case GL_COMPUTE_SHADER:
Olli Etuahocce89652017-06-19 16:04:09 +0300623 symbolTable.setDefaultPrecision(EbtInt, EbpHigh);
624 symbolTable.setDefaultPrecision(EbtFloat, EbpHigh);
Jamie Madillacb4b812016-11-07 13:50:29 -0500625 break;
626 default:
627 assert(false && "Language not supported");
Nicolas Capens49a88872013-06-20 09:54:03 -0400628 }
Olli Etuaho183d7e22015-11-20 15:59:09 +0200629 // Set defaults for sampler types that have default precision, even those that are
Zhenyao Moa5a1dfc2013-09-23 14:57:03 -0400630 // only available if an extension exists.
Olli Etuaho183d7e22015-11-20 15:59:09 +0200631 // New sampler types in ESSL3 don't have default precision. ESSL1 types do.
632 initSamplerDefaultPrecision(EbtSampler2D);
633 initSamplerDefaultPrecision(EbtSamplerCube);
634 // SamplerExternalOES is specified in the extension to have default precision.
635 initSamplerDefaultPrecision(EbtSamplerExternalOES);
Andrei Volykhina5527072017-03-22 16:46:30 +0300636 // SamplerExternal2DY2YEXT is specified in the extension to have default precision.
637 initSamplerDefaultPrecision(EbtSamplerExternal2DY2YEXT);
Olli Etuaho183d7e22015-11-20 15:59:09 +0200638 // It isn't specified whether Sampler2DRect has default precision.
639 initSamplerDefaultPrecision(EbtSampler2DRect);
Nicolas Capens49a88872013-06-20 09:54:03 -0400640
Olli Etuahocce89652017-06-19 16:04:09 +0300641 symbolTable.setDefaultPrecision(EbtAtomicCounter, EbpHigh);
jchen104cdac9e2017-05-08 11:01:20 +0800642
Jamie Madill1b452142013-07-12 14:51:11 -0400643 InsertBuiltInFunctions(shaderType, shaderSpec, resources, symbolTable);
Nicolas Capens49a88872013-06-20 09:54:03 -0400644
645 IdentifyBuiltIns(shaderType, shaderSpec, resources, symbolTable);
646
647 return true;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000648}
649
Olli Etuaho183d7e22015-11-20 15:59:09 +0200650void TCompiler::initSamplerDefaultPrecision(TBasicType samplerType)
651{
652 ASSERT(samplerType > EbtGuardSamplerBegin && samplerType < EbtGuardSamplerEnd);
Olli Etuahocce89652017-06-19 16:04:09 +0300653 symbolTable.setDefaultPrecision(samplerType, EbpLow);
Olli Etuaho183d7e22015-11-20 15:59:09 +0200654}
655
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400656void TCompiler::setResourceString()
657{
658 std::ostringstream strstream;
Geoff Langb66a9092016-05-16 15:59:14 -0400659
660 // clang-format off
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400661 strstream << ":MaxVertexAttribs:" << compileResources.MaxVertexAttribs
Jamie Madillacb4b812016-11-07 13:50:29 -0500662 << ":MaxVertexUniformVectors:" << compileResources.MaxVertexUniformVectors
663 << ":MaxVaryingVectors:" << compileResources.MaxVaryingVectors
664 << ":MaxVertexTextureImageUnits:" << compileResources.MaxVertexTextureImageUnits
665 << ":MaxCombinedTextureImageUnits:" << compileResources.MaxCombinedTextureImageUnits
666 << ":MaxTextureImageUnits:" << compileResources.MaxTextureImageUnits
667 << ":MaxFragmentUniformVectors:" << compileResources.MaxFragmentUniformVectors
668 << ":MaxDrawBuffers:" << compileResources.MaxDrawBuffers
669 << ":OES_standard_derivatives:" << compileResources.OES_standard_derivatives
670 << ":OES_EGL_image_external:" << compileResources.OES_EGL_image_external
671 << ":OES_EGL_image_external_essl3:" << compileResources.OES_EGL_image_external_essl3
672 << ":NV_EGL_stream_consumer_external:" << compileResources.NV_EGL_stream_consumer_external
673 << ":ARB_texture_rectangle:" << compileResources.ARB_texture_rectangle
674 << ":EXT_draw_buffers:" << compileResources.EXT_draw_buffers
675 << ":FragmentPrecisionHigh:" << compileResources.FragmentPrecisionHigh
676 << ":MaxExpressionComplexity:" << compileResources.MaxExpressionComplexity
677 << ":MaxCallStackDepth:" << compileResources.MaxCallStackDepth
678 << ":MaxFunctionParameters:" << compileResources.MaxFunctionParameters
679 << ":EXT_blend_func_extended:" << compileResources.EXT_blend_func_extended
680 << ":EXT_frag_depth:" << compileResources.EXT_frag_depth
681 << ":EXT_shader_texture_lod:" << compileResources.EXT_shader_texture_lod
682 << ":EXT_shader_framebuffer_fetch:" << compileResources.EXT_shader_framebuffer_fetch
683 << ":NV_shader_framebuffer_fetch:" << compileResources.NV_shader_framebuffer_fetch
684 << ":ARM_shader_framebuffer_fetch:" << compileResources.ARM_shader_framebuffer_fetch
Andrei Volykhina5527072017-03-22 16:46:30 +0300685 << ":EXT_YUV_target:" << compileResources.EXT_YUV_target
Jamie Madillacb4b812016-11-07 13:50:29 -0500686 << ":MaxVertexOutputVectors:" << compileResources.MaxVertexOutputVectors
687 << ":MaxFragmentInputVectors:" << compileResources.MaxFragmentInputVectors
688 << ":MinProgramTexelOffset:" << compileResources.MinProgramTexelOffset
689 << ":MaxProgramTexelOffset:" << compileResources.MaxProgramTexelOffset
690 << ":MaxDualSourceDrawBuffers:" << compileResources.MaxDualSourceDrawBuffers
691 << ":NV_draw_buffers:" << compileResources.NV_draw_buffers
692 << ":WEBGL_debug_shader_precision:" << compileResources.WEBGL_debug_shader_precision
693 << ":MaxImageUnits:" << compileResources.MaxImageUnits
694 << ":MaxVertexImageUniforms:" << compileResources.MaxVertexImageUniforms
695 << ":MaxFragmentImageUniforms:" << compileResources.MaxFragmentImageUniforms
696 << ":MaxComputeImageUniforms:" << compileResources.MaxComputeImageUniforms
697 << ":MaxCombinedImageUniforms:" << compileResources.MaxCombinedImageUniforms
698 << ":MaxCombinedShaderOutputResources:" << compileResources.MaxCombinedShaderOutputResources
699 << ":MaxComputeWorkGroupCountX:" << compileResources.MaxComputeWorkGroupCount[0]
700 << ":MaxComputeWorkGroupCountY:" << compileResources.MaxComputeWorkGroupCount[1]
701 << ":MaxComputeWorkGroupCountZ:" << compileResources.MaxComputeWorkGroupCount[2]
702 << ":MaxComputeWorkGroupSizeX:" << compileResources.MaxComputeWorkGroupSize[0]
703 << ":MaxComputeWorkGroupSizeY:" << compileResources.MaxComputeWorkGroupSize[1]
704 << ":MaxComputeWorkGroupSizeZ:" << compileResources.MaxComputeWorkGroupSize[2]
705 << ":MaxComputeUniformComponents:" << compileResources.MaxComputeUniformComponents
706 << ":MaxComputeTextureImageUnits:" << compileResources.MaxComputeTextureImageUnits
707 << ":MaxComputeAtomicCounters:" << compileResources.MaxComputeAtomicCounters
708 << ":MaxComputeAtomicCounterBuffers:" << compileResources.MaxComputeAtomicCounterBuffers
709 << ":MaxVertexAtomicCounters:" << compileResources.MaxVertexAtomicCounters
710 << ":MaxFragmentAtomicCounters:" << compileResources.MaxFragmentAtomicCounters
711 << ":MaxCombinedAtomicCounters:" << compileResources.MaxCombinedAtomicCounters
712 << ":MaxAtomicCounterBindings:" << compileResources.MaxAtomicCounterBindings
713 << ":MaxVertexAtomicCounterBuffers:" << compileResources.MaxVertexAtomicCounterBuffers
714 << ":MaxFragmentAtomicCounterBuffers:" << compileResources.MaxFragmentAtomicCounterBuffers
715 << ":MaxCombinedAtomicCounterBuffers:" << compileResources.MaxCombinedAtomicCounterBuffers
716 << ":MaxAtomicCounterBufferSize:" << compileResources.MaxAtomicCounterBufferSize;
Geoff Langb66a9092016-05-16 15:59:14 -0400717 // clang-format on
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400718
719 builtInResourcesString = strstream.str();
720}
721
alokp@chromium.org07620a52010-09-23 17:53:56 +0000722void TCompiler::clearResults()
723{
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000724 arrayBoundsClamper.Cleanup();
alokp@chromium.org07620a52010-09-23 17:53:56 +0000725 infoSink.info.erase();
726 infoSink.obj.erase();
727 infoSink.debug.erase();
Olli Etuaho77ba4082016-12-16 12:01:18 +0000728 mDiagnostics.resetErrorCount();
alokp@chromium.org07620a52010-09-23 17:53:56 +0000729
Jamie Madilled27c722014-07-02 15:31:23 -0400730 attributes.clear();
731 outputVariables.clear();
alokp@chromium.org07620a52010-09-23 17:53:56 +0000732 uniforms.clear();
Zhenyao Mod2d340b2013-09-23 14:57:05 -0400733 varyings.clear();
Jamie Madilled27c722014-07-02 15:31:23 -0400734 interfaceBlocks.clear();
Kenneth Russellbccc65d2016-07-19 16:48:43 -0700735 variablesCollected = false;
Olli Etuahob12040c2017-06-27 14:20:45 +0300736 mGLPositionInitialized = false;
zmo@google.coma3b4ab42011-09-16 00:53:26 +0000737
Olli Etuaho09b04a22016-12-15 13:30:26 +0000738 mNumViews = -1;
739
Olli Etuahodfa75e82017-01-23 09:43:06 -0800740 builtInFunctionEmulator.cleanup();
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +0000741
742 nameMap.clear();
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200743
Yunchao Hed7297bf2017-04-19 15:27:10 +0800744 mSourcePath = nullptr;
Olli Etuaho4dd06d52017-07-05 12:41:06 +0300745 mTemporaryId = TSymbolUniqueId();
alokp@chromium.org07620a52010-09-23 17:53:56 +0000746}
747
Corentin Wallez71d147f2015-02-11 11:15:24 -0800748bool TCompiler::initCallDag(TIntermNode *root)
zmo@google.comb1762df2011-07-30 02:04:23 +0000749{
Corentin Wallez71d147f2015-02-11 11:15:24 -0800750 mCallDag.clear();
751
Olli Etuaho77ba4082016-12-16 12:01:18 +0000752 switch (mCallDag.init(root, &mDiagnostics))
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800753 {
Jamie Madillacb4b812016-11-07 13:50:29 -0500754 case CallDAG::INITDAG_SUCCESS:
755 return true;
756 case CallDAG::INITDAG_RECURSION:
Jamie Madillacb4b812016-11-07 13:50:29 -0500757 case CallDAG::INITDAG_UNDEFINED:
Olli Etuaho77ba4082016-12-16 12:01:18 +0000758 // Error message has already been written out.
759 ASSERT(mDiagnostics.numErrors() > 0);
Jamie Madillacb4b812016-11-07 13:50:29 -0500760 return false;
Corentin Wallez71d147f2015-02-11 11:15:24 -0800761 }
762
763 UNREACHABLE();
764 return true;
765}
766
767bool TCompiler::checkCallDepth()
768{
769 std::vector<int> depths(mCallDag.size());
770
771 for (size_t i = 0; i < mCallDag.size(); i++)
772 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500773 int depth = 0;
Corentin Wallez71d147f2015-02-11 11:15:24 -0800774 auto &record = mCallDag.getRecordFromIndex(i);
775
776 for (auto &calleeIndex : record.callees)
777 {
778 depth = std::max(depth, depths[calleeIndex] + 1);
779 }
780
781 depths[i] = depth;
782
783 if (depth >= maxCallStackDepth)
784 {
785 // Trace back the function chain to have a meaningful info log.
Olli Etuaho77ba4082016-12-16 12:01:18 +0000786 std::stringstream errorStream;
787 errorStream << "Call stack too deep (larger than " << maxCallStackDepth
788 << ") with the following call chain: " << record.name;
Corentin Wallez71d147f2015-02-11 11:15:24 -0800789
Cooper Partin4d61f7e2015-08-12 10:56:50 -0700790 int currentFunction = static_cast<int>(i);
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500791 int currentDepth = depth;
Corentin Wallez71d147f2015-02-11 11:15:24 -0800792
793 while (currentFunction != -1)
794 {
Olli Etuaho77ba4082016-12-16 12:01:18 +0000795 errorStream << " -> " << mCallDag.getRecordFromIndex(currentFunction).name;
Corentin Wallez71d147f2015-02-11 11:15:24 -0800796
797 int nextFunction = -1;
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500798 for (auto &calleeIndex : mCallDag.getRecordFromIndex(currentFunction).callees)
Corentin Wallez71d147f2015-02-11 11:15:24 -0800799 {
800 if (depths[calleeIndex] == currentDepth - 1)
801 {
802 currentDepth--;
803 nextFunction = calleeIndex;
804 }
805 }
806
807 currentFunction = nextFunction;
808 }
809
Olli Etuaho77ba4082016-12-16 12:01:18 +0000810 std::string errorStr = errorStream.str();
811 mDiagnostics.globalError(errorStr.c_str());
812
Corentin Wallez71d147f2015-02-11 11:15:24 -0800813 return false;
814 }
815 }
816
817 return true;
818}
819
820bool TCompiler::tagUsedFunctions()
821{
822 // Search from main, starting from the end of the DAG as it usually is the root.
Cooper Partin4d61f7e2015-08-12 10:56:50 -0700823 for (size_t i = mCallDag.size(); i-- > 0;)
Corentin Wallez71d147f2015-02-11 11:15:24 -0800824 {
Olli Etuahoec9232b2017-03-27 17:01:37 +0300825 if (mCallDag.getRecordFromIndex(i).name == "main")
Corentin Wallez71d147f2015-02-11 11:15:24 -0800826 {
827 internalTagUsedFunction(i);
828 return true;
829 }
830 }
831
Olli Etuaho77ba4082016-12-16 12:01:18 +0000832 mDiagnostics.globalError("Missing main()");
Corentin Wallez71d147f2015-02-11 11:15:24 -0800833 return false;
834}
835
836void TCompiler::internalTagUsedFunction(size_t index)
837{
838 if (functionMetadata[index].used)
839 {
840 return;
841 }
842
843 functionMetadata[index].used = true;
844
845 for (int calleeIndex : mCallDag.getRecordFromIndex(index).callees)
846 {
847 internalTagUsedFunction(calleeIndex);
zmo@google.comb1762df2011-07-30 02:04:23 +0000848 }
849}
850
Corentin Walleza094a8a2015-04-07 11:53:06 -0700851// A predicate for the stl that returns if a top-level node is unused
852class TCompiler::UnusedPredicate
853{
854 public:
855 UnusedPredicate(const CallDAG *callDag, const std::vector<FunctionMetadata> *metadatas)
Jamie Madillacb4b812016-11-07 13:50:29 -0500856 : mCallDag(callDag), mMetadatas(metadatas)
Corentin Walleza094a8a2015-04-07 11:53:06 -0700857 {
858 }
859
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500860 bool operator()(TIntermNode *node)
Corentin Walleza094a8a2015-04-07 11:53:06 -0700861 {
Olli Etuaho16c745a2017-01-16 17:02:27 +0000862 const TIntermFunctionPrototype *asFunctionPrototype = node->getAsFunctionPrototypeNode();
863 const TIntermFunctionDefinition *asFunctionDefinition = node->getAsFunctionDefinition();
Corentin Walleza094a8a2015-04-07 11:53:06 -0700864
Olli Etuaho336b1472016-10-05 16:37:55 +0100865 const TFunctionSymbolInfo *functionInfo = nullptr;
866
Olli Etuaho16c745a2017-01-16 17:02:27 +0000867 if (asFunctionDefinition)
Olli Etuaho336b1472016-10-05 16:37:55 +0100868 {
Olli Etuaho16c745a2017-01-16 17:02:27 +0000869 functionInfo = asFunctionDefinition->getFunctionSymbolInfo();
Olli Etuaho336b1472016-10-05 16:37:55 +0100870 }
Olli Etuaho16c745a2017-01-16 17:02:27 +0000871 else if (asFunctionPrototype)
Olli Etuaho336b1472016-10-05 16:37:55 +0100872 {
Olli Etuaho16c745a2017-01-16 17:02:27 +0000873 functionInfo = asFunctionPrototype->getFunctionSymbolInfo();
Olli Etuaho336b1472016-10-05 16:37:55 +0100874 }
875 if (functionInfo == nullptr)
Corentin Walleza094a8a2015-04-07 11:53:06 -0700876 {
877 return false;
878 }
879
Olli Etuaho336b1472016-10-05 16:37:55 +0100880 size_t callDagIndex = mCallDag->findIndex(functionInfo);
Corentin Walleza094a8a2015-04-07 11:53:06 -0700881 if (callDagIndex == CallDAG::InvalidIndex)
882 {
883 // This happens only for unimplemented prototypes which are thus unused
Olli Etuaho16c745a2017-01-16 17:02:27 +0000884 ASSERT(asFunctionPrototype);
Corentin Walleza094a8a2015-04-07 11:53:06 -0700885 return true;
886 }
887
888 ASSERT(callDagIndex < mMetadatas->size());
889 return !(*mMetadatas)[callDagIndex].used;
890 }
891
892 private:
893 const CallDAG *mCallDag;
894 const std::vector<FunctionMetadata> *mMetadatas;
895};
896
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100897bool TCompiler::pruneUnusedFunctions(TIntermBlock *root)
Corentin Walleza094a8a2015-04-07 11:53:06 -0700898{
Corentin Walleza094a8a2015-04-07 11:53:06 -0700899 UnusedPredicate isUnused(&mCallDag, &functionMetadata);
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100900 TIntermSequence *sequence = root->getSequence();
Corentin Wallezb081e782015-07-20 05:40:04 -0700901
902 if (!sequence->empty())
903 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500904 sequence->erase(std::remove_if(sequence->begin(), sequence->end(), isUnused),
905 sequence->end());
Corentin Wallezb081e782015-07-20 05:40:04 -0700906 }
Corentin Walleza094a8a2015-04-07 11:53:06 -0700907
908 return true;
909}
910
Olli Etuahob4279202017-05-18 15:08:24 +0300911bool TCompiler::limitExpressionComplexity(TIntermBlock *root)
Jamie Madilleb1a0102013-07-08 13:31:38 -0400912{
Jamie Madillacb4b812016-11-07 13:50:29 -0500913 TMaxDepthTraverser traverser(maxExpressionComplexity + 1);
Jamie Madilleb1a0102013-07-08 13:31:38 -0400914 root->traverse(&traverser);
Jamie Madill6654bc92014-03-26 14:01:57 -0400915
916 if (traverser.getMaxDepth() > maxExpressionComplexity)
917 {
Olli Etuaho77ba4082016-12-16 12:01:18 +0000918 mDiagnostics.globalError("Expression too complex.");
Jamie Madill6654bc92014-03-26 14:01:57 -0400919 return false;
920 }
921
Olli Etuahob4279202017-05-18 15:08:24 +0300922 if (!ValidateMaxParameters(root, maxFunctionParameters))
Olli Etuaho19d1dc92016-03-08 17:18:46 +0200923 {
Olli Etuaho77ba4082016-12-16 12:01:18 +0000924 mDiagnostics.globalError("Function has too many parameters.");
Olli Etuaho19d1dc92016-03-08 17:18:46 +0200925 return false;
926 }
927
Jamie Madilleb1a0102013-07-08 13:31:38 -0400928 return true;
929}
930
Corentin Wallez1df16022016-10-27 08:16:56 -0400931bool TCompiler::shouldCollectVariables(ShCompileOptions compileOptions)
932{
933 return (compileOptions & SH_VARIABLES) != 0;
934}
935
936bool TCompiler::wereVariablesCollected() const
937{
938 return variablesCollected;
939}
940
Olli Etuaho9cbc07c2017-05-10 18:22:01 +0300941void TCompiler::initializeGLPosition(TIntermBlock *root)
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800942{
Zhenyao Mo72111912016-07-20 17:45:56 -0700943 InitVariableList list;
944 sh::ShaderVariable var(GL_FLOAT_VEC4, 0);
945 var.name = "gl_Position";
946 list.push_back(var);
Martin Radeve145def2017-06-22 12:49:12 +0300947 InitializeVariables(root, list, symbolTable, shaderVersion, shaderSpec, extensionBehavior);
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800948}
949
Olli Etuaho9cbc07c2017-05-10 18:22:01 +0300950void TCompiler::useAllMembersInUnusedStandardAndSharedBlocks(TIntermBlock *root)
Qin Jiajia7835b522016-10-08 11:20:17 +0800951{
952 sh::InterfaceBlockList list;
953
954 for (auto block : interfaceBlocks)
955 {
956 if (!block.staticUse &&
957 (block.layout == sh::BLOCKLAYOUT_STANDARD || block.layout == sh::BLOCKLAYOUT_SHARED))
958 {
959 list.push_back(block);
960 }
961 }
962
Zhenyao Mod7490962016-11-09 15:49:51 -0800963 sh::UseInterfaceBlockFields(root, list, symbolTable);
Qin Jiajia7835b522016-10-08 11:20:17 +0800964}
965
Olli Etuaho9cbc07c2017-05-10 18:22:01 +0300966void TCompiler::initializeOutputVariables(TIntermBlock *root)
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800967{
Zhenyao Mo72111912016-07-20 17:45:56 -0700968 InitVariableList list;
969 if (shaderType == GL_VERTEX_SHADER)
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800970 {
Zhenyao Mo72111912016-07-20 17:45:56 -0700971 for (auto var : varyings)
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800972 {
Zhenyao Mof9312682016-07-22 12:51:31 -0700973 list.push_back(var);
Olli Etuahob12040c2017-06-27 14:20:45 +0300974 if (var.name == "gl_Position")
975 {
976 ASSERT(!mGLPositionInitialized);
977 mGLPositionInitialized = true;
978 }
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800979 }
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800980 }
Zhenyao Mo72111912016-07-20 17:45:56 -0700981 else
982 {
983 ASSERT(shaderType == GL_FRAGMENT_SHADER);
984 for (auto var : outputVariables)
985 {
986 list.push_back(var);
987 }
988 }
Martin Radeve145def2017-06-22 12:49:12 +0300989 InitializeVariables(root, list, symbolTable, shaderVersion, shaderSpec, extensionBehavior);
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800990}
991
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500992const TExtensionBehavior &TCompiler::getExtensionBehavior() const
zmo@google.com5601ea02011-06-10 18:23:25 +0000993{
994 return extensionBehavior;
995}
zmo@google.com32e97312011-08-24 01:03:11 +0000996
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200997const char *TCompiler::getSourcePath() const
998{
999 return mSourcePath;
1000}
1001
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001002const ShBuiltInResources &TCompiler::getResources() const
shannon.woods%transgaming.com@gtempaccount.com18b4c4b2013-04-13 03:31:40 +00001003{
1004 return compileResources;
1005}
1006
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001007const ArrayBoundsClamper &TCompiler::getArrayBoundsClamper() const
daniel@transgaming.com4167cc92013-01-11 04:11:53 +00001008{
1009 return arrayBoundsClamper;
1010}
1011
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +00001012ShArrayIndexClampingStrategy TCompiler::getArrayIndexClampingStrategy() const
1013{
1014 return clampingStrategy;
1015}
1016
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001017const BuiltInFunctionEmulator &TCompiler::getBuiltInFunctionEmulator() const
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +00001018{
1019 return builtInFunctionEmulator;
1020}
Zhenyao Mo94ac7b72014-10-15 18:22:08 -07001021
Qiankun Miao7ebb97f2016-09-08 18:01:50 +08001022void TCompiler::writePragma(ShCompileOptions compileOptions)
Zhenyao Mo94ac7b72014-10-15 18:22:08 -07001023{
Kenneth Russellbccc65d2016-07-19 16:48:43 -07001024 if (!(compileOptions & SH_FLATTEN_PRAGMA_STDGL_INVARIANT_ALL))
1025 {
1026 TInfoSinkBase &sink = infoSink.obj;
1027 if (mPragma.stdgl.invariantAll)
1028 sink << "#pragma STDGL invariant(all)\n";
1029 }
1030}
1031
1032bool TCompiler::isVaryingDefined(const char *varyingName)
1033{
1034 ASSERT(variablesCollected);
1035 for (size_t ii = 0; ii < varyings.size(); ++ii)
1036 {
1037 if (varyings[ii].name == varyingName)
1038 {
1039 return true;
1040 }
1041 }
1042
1043 return false;
Zhenyao Mo94ac7b72014-10-15 18:22:08 -07001044}
Jamie Madillacb4b812016-11-07 13:50:29 -05001045
1046} // namespace sh