blob: ab89e9a8722c3d653ae48191193bd66bd1a9efb8 [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 Etuahoab918822017-07-14 17:03:42 +030016#include "compiler/translator/ClampPointSize.h"
Martin Radev69056a12017-05-18 11:14:50 +030017#include "compiler/translator/DeclareAndInitBuiltinsForInstancedMultiview.h"
Olli Etuaho3d932d82016-04-12 11:10:30 +030018#include "compiler/translator/DeferGlobalInitializers.h"
Zhenyao Mo4e94fea2016-08-09 14:31:37 -070019#include "compiler/translator/EmulateGLFragColorBroadcast.h"
Jamie Madilld5696192016-10-06 11:09:24 -040020#include "compiler/translator/EmulatePrecision.h"
Geoff Lang17732822013-08-29 13:46:49 -040021#include "compiler/translator/Initialize.h"
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080022#include "compiler/translator/InitializeVariables.h"
Olli Etuaho9733cee2017-05-11 19:14:35 +030023#include "compiler/translator/IntermNodePatternMatcher.h"
Olli Etuahocccf2b02017-07-05 14:50:54 +030024#include "compiler/translator/IsASTDepthBelowLimit.h"
25#include "compiler/translator/OutputTree.h"
Jamie Madill6b9cb252013-10-17 10:45:47 -040026#include "compiler/translator/ParseContext.h"
Olli Etuahoc6833112015-04-22 15:15:54 +030027#include "compiler/translator/PruneEmptyDeclarations.h"
Zhenyao Moe740add2014-07-18 17:01:01 -070028#include "compiler/translator/RegenerateStructNames.h"
Qiankun Miao705a9192016-08-29 10:05:27 +080029#include "compiler/translator/RemoveInvariantDeclaration.h"
Olli Etuaho5c407bb2015-06-01 12:20:39 +030030#include "compiler/translator/RemovePow.h"
Corentin Wallezd4b50542015-09-28 12:19:26 -070031#include "compiler/translator/RewriteDoWhile.h"
Zhenyao Mocd68fe72014-07-11 10:45:44 -070032#include "compiler/translator/ScalarizeVecAndMatConstructorArgs.h"
Olli Etuaho9733cee2017-05-11 19:14:35 +030033#include "compiler/translator/SeparateDeclarations.h"
34#include "compiler/translator/SimplifyLoopConditions.h"
Zhenyao Mo7cab38b2013-10-15 12:59:30 -070035#include "compiler/translator/UnfoldShortCircuitAST.h"
Qin Jiajia7835b522016-10-08 11:20:17 +080036#include "compiler/translator/UseInterfaceBlockFields.h"
Geoff Lang17732822013-08-29 13:46:49 -040037#include "compiler/translator/ValidateLimitations.h"
Olli Etuaho19d1dc92016-03-08 17:18:46 +020038#include "compiler/translator/ValidateMaxParameters.h"
Olli Etuaho09b04a22016-12-15 13:30:26 +000039#include "compiler/translator/ValidateMultiviewWebGL.h"
Geoff Lang17732822013-08-29 13:46:49 -040040#include "compiler/translator/ValidateOutputs.h"
41#include "compiler/translator/VariablePacker.h"
shannon.woods@transgaming.comda1ed362013-01-25 21:54:57 +000042#include "third_party/compiler/ArrayBoundsClamper.h"
Corentin Wallez28b65282016-06-16 07:24:50 -070043
Jamie Madillacb4b812016-11-07 13:50:29 -050044namespace sh
45{
46
Corentin Wallez28b65282016-06-16 07:24:50 -070047namespace
48{
49
50#if defined(ANGLE_ENABLE_FUZZER_CORPUS_OUTPUT)
51void DumpFuzzerCase(char const *const *shaderStrings,
52 size_t numStrings,
53 uint32_t type,
54 uint32_t spec,
55 uint32_t output,
56 uint64_t options)
57{
58 static int fileIndex = 0;
59
60 std::ostringstream o;
61 o << "corpus/" << fileIndex++ << ".sample";
62 std::string s = o.str();
63
64 // Must match the input format of the fuzzer
65 FILE *f = fopen(s.c_str(), "w");
66 fwrite(&type, sizeof(type), 1, f);
67 fwrite(&spec, sizeof(spec), 1, f);
68 fwrite(&output, sizeof(output), 1, f);
69 fwrite(&options, sizeof(options), 1, f);
70
71 char zero[128 - 20] = {0};
72 fwrite(&zero, 128 - 20, 1, f);
73
74 for (size_t i = 0; i < numStrings; i++)
75 {
76 fwrite(shaderStrings[i], sizeof(char), strlen(shaderStrings[i]), f);
77 }
78 fwrite(&zero, 1, 1, f);
79
80 fclose(f);
81}
82#endif // defined(ANGLE_ENABLE_FUZZER_CORPUS_OUTPUT)
83} // anonymous namespace
84
Jamie Madill5508f392014-02-20 13:31:36 -050085bool IsWebGLBasedSpec(ShShaderSpec spec)
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +000086{
Qiankun Miaoc2c5fc42016-08-31 15:24:22 +080087 return (spec == SH_WEBGL_SPEC || spec == SH_WEBGL2_SPEC || spec == SH_WEBGL3_SPEC);
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +000088}
89
Qingqing Dengad0d0792015-04-08 14:25:06 -070090bool IsGLSL130OrNewer(ShShaderOutput output)
91{
Jamie Madillacb4b812016-11-07 13:50:29 -050092 return (output == SH_GLSL_130_OUTPUT || output == SH_GLSL_140_OUTPUT ||
93 output == SH_GLSL_150_CORE_OUTPUT || output == SH_GLSL_330_CORE_OUTPUT ||
94 output == SH_GLSL_400_CORE_OUTPUT || output == SH_GLSL_410_CORE_OUTPUT ||
95 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);
Qingqing Dengad0d0792015-04-08 14:25:06 -070097}
98
Qiankun Miao705a9192016-08-29 10:05:27 +080099bool IsGLSL420OrNewer(ShShaderOutput output)
100{
Jamie Madillacb4b812016-11-07 13:50:29 -0500101 return (output == SH_GLSL_420_CORE_OUTPUT || output == SH_GLSL_430_CORE_OUTPUT ||
102 output == SH_GLSL_440_CORE_OUTPUT || output == SH_GLSL_450_CORE_OUTPUT);
Qiankun Miao705a9192016-08-29 10:05:27 +0800103}
104
Zhenyao Mob7bf7422016-11-08 14:44:05 -0800105bool IsGLSL410OrOlder(ShShaderOutput output)
106{
107 return (output == SH_GLSL_130_OUTPUT || output == SH_GLSL_140_OUTPUT ||
108 output == SH_GLSL_150_CORE_OUTPUT || output == SH_GLSL_330_CORE_OUTPUT ||
109 output == SH_GLSL_400_CORE_OUTPUT || output == SH_GLSL_410_CORE_OUTPUT);
110}
111
Qiankun Miao89dd8f32016-11-09 12:59:30 +0000112bool RemoveInvariant(sh::GLenum shaderType,
113 int shaderVersion,
114 ShShaderOutput outputType,
115 ShCompileOptions compileOptions)
116{
117 if ((compileOptions & SH_DONT_REMOVE_INVARIANT_FOR_FRAGMENT_INPUT) == 0 &&
118 shaderType == GL_FRAGMENT_SHADER && IsGLSL420OrNewer(outputType))
119 return true;
120
121 if ((compileOptions & SH_REMOVE_INVARIANT_AND_CENTROID_FOR_ESSL3) != 0 &&
Qiankun Miao41f9f672016-11-16 17:04:36 +0800122 shaderVersion >= 300 && shaderType == GL_VERTEX_SHADER)
Qiankun Miao89dd8f32016-11-09 12:59:30 +0000123 return true;
124
125 return false;
126}
127
Zhenyao Mo7faf1a12014-04-25 18:03:56 -0700128size_t GetGlobalMaxTokenSize(ShShaderSpec spec)
Jamie Madill88f6e942014-02-19 10:27:53 -0500129{
He Yunchao29ab9ff2015-08-06 16:58:30 +0800130 // WebGL defines a max token length of 256, while ES2 leaves max token
Jamie Madill88f6e942014-02-19 10:27:53 -0500131 // size undefined. ES3 defines a max size of 1024 characters.
Zhenyao Modb9b40b2014-10-29 15:00:04 -0700132 switch (spec)
Jamie Madill88f6e942014-02-19 10:27:53 -0500133 {
Jamie Madillacb4b812016-11-07 13:50:29 -0500134 case SH_WEBGL_SPEC:
135 return 256;
136 default:
137 return 1024;
Jamie Madill88f6e942014-02-19 10:27:53 -0500138 }
139}
140
Shao77891c02017-06-23 16:30:17 +0800141int GetMaxUniformVectorsForShaderType(GLenum shaderType, const ShBuiltInResources &resources)
142{
143 switch (shaderType)
144 {
145 case GL_VERTEX_SHADER:
146 return resources.MaxVertexUniformVectors;
147 case GL_FRAGMENT_SHADER:
148 return resources.MaxFragmentUniformVectors;
149 case GL_COMPUTE_SHADER:
150 // TODO (jiawei.shao@intel.com): check if we need finer-grained component counting
151 return resources.MaxComputeUniformComponents / 4;
152 default:
153 UNIMPLEMENTED();
154 return -1;
155 }
156}
157
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500158namespace
159{
Zhenyao Modb9b40b2014-10-29 15:00:04 -0700160
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800161class TScopedPoolAllocator
162{
163 public:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500164 TScopedPoolAllocator(TPoolAllocator *allocator) : mAllocator(allocator)
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800165 {
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400166 mAllocator->push();
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000167 SetGlobalPoolAllocator(mAllocator);
168 }
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800169 ~TScopedPoolAllocator()
170 {
Yunchao Hef81ce4a2017-04-24 10:49:17 +0800171 SetGlobalPoolAllocator(nullptr);
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400172 mAllocator->pop();
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000173 }
174
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800175 private:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500176 TPoolAllocator *mAllocator;
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400177};
178
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800179class TScopedSymbolTableLevel
180{
181 public:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500182 TScopedSymbolTableLevel(TSymbolTable *table) : mTable(table)
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800183 {
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400184 ASSERT(mTable->atBuiltInLevel());
185 mTable->push();
186 }
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800187 ~TScopedSymbolTableLevel()
188 {
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400189 while (!mTable->atBuiltInLevel())
190 mTable->pop();
191 }
192
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800193 private:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500194 TSymbolTable *mTable;
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000195};
Zhenyao Modb9b40b2014-10-29 15:00:04 -0700196
197int MapSpecToShaderVersion(ShShaderSpec spec)
198{
199 switch (spec)
200 {
Jamie Madillacb4b812016-11-07 13:50:29 -0500201 case SH_GLES2_SPEC:
202 case SH_WEBGL_SPEC:
203 return 100;
204 case SH_GLES3_SPEC:
205 case SH_WEBGL2_SPEC:
206 return 300;
207 case SH_GLES3_1_SPEC:
208 case SH_WEBGL3_SPEC:
209 return 310;
210 default:
211 UNREACHABLE();
212 return 0;
Zhenyao Modb9b40b2014-10-29 15:00:04 -0700213 }
214}
215
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000216} // namespace
217
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800218TShHandleBase::TShHandleBase()
219{
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000220 allocator.push();
221 SetGlobalPoolAllocator(&allocator);
222}
223
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800224TShHandleBase::~TShHandleBase()
225{
Yunchao Hef81ce4a2017-04-24 10:49:17 +0800226 SetGlobalPoolAllocator(nullptr);
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000227 allocator.popAll();
228}
229
Jamie Madill183bde52014-07-02 15:31:19 -0400230TCompiler::TCompiler(sh::GLenum type, ShShaderSpec spec, ShShaderOutput output)
Kenneth Russellbccc65d2016-07-19 16:48:43 -0700231 : variablesCollected(false),
Olli Etuahob12040c2017-06-27 14:20:45 +0300232 mGLPositionInitialized(false),
Kenneth Russellbccc65d2016-07-19 16:48:43 -0700233 shaderType(type),
zmo@google.comf420c422011-09-12 18:27:59 +0000234 shaderSpec(spec),
Jamie Madill68fe74a2014-05-27 12:56:01 -0400235 outputType(output),
Jamie Madilleb1a0102013-07-08 13:31:38 -0400236 maxUniformVectors(0),
237 maxExpressionComplexity(0),
238 maxCallStackDepth(0),
Olli Etuaho19d1dc92016-03-08 17:18:46 +0200239 maxFunctionParameters(0),
shannon.woods%transgaming.com@gtempaccount.comcbb6b6a2013-04-13 03:27:47 +0000240 fragmentPrecisionHigh(false),
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000241 clampingStrategy(SH_CLAMP_WITH_CLAMP_INTRINSIC),
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200242 builtInFunctionEmulator(),
Olli Etuaho77ba4082016-12-16 12:01:18 +0000243 mDiagnostics(infoSink.info),
Yunchao Hef81ce4a2017-04-24 10:49:17 +0800244 mSourcePath(nullptr),
Olli Etuahoa5e693a2017-07-13 16:07:26 +0300245 mComputeShaderLocalSizeDeclared(false)
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000246{
Martin Radev802abe02016-08-04 17:48:32 +0300247 mComputeShaderLocalSize.fill(1);
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000248}
249
250TCompiler::~TCompiler()
251{
252}
253
Qiankun Miao7ebb97f2016-09-08 18:01:50 +0800254bool TCompiler::shouldRunLoopAndIndexingValidation(ShCompileOptions compileOptions) const
Olli Etuaho5d91dda2015-06-18 15:47:46 +0300255{
256 // If compiling an ESSL 1.00 shader for WebGL, or if its been requested through the API,
257 // validate loop and indexing as well (to verify that the shader only uses minimal functionality
258 // of ESSL 1.00 as in Appendix A of the spec).
259 return (IsWebGLBasedSpec(shaderSpec) && shaderVersion == 100) ||
260 (compileOptions & SH_VALIDATE_LOOP_INDEXING);
261}
262
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500263bool TCompiler::Init(const ShBuiltInResources &resources)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000264{
Shao77891c02017-06-23 16:30:17 +0800265 shaderVersion = 100;
266
267 maxUniformVectors = GetMaxUniformVectorsForShaderType(shaderType, resources);
268
Jamie Madilleb1a0102013-07-08 13:31:38 -0400269 maxExpressionComplexity = resources.MaxExpressionComplexity;
Olli Etuaho19d1dc92016-03-08 17:18:46 +0200270 maxCallStackDepth = resources.MaxCallStackDepth;
271 maxFunctionParameters = resources.MaxFunctionParameters;
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400272
273 SetGlobalPoolAllocator(&allocator);
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000274
alokp@chromium.org07620a52010-09-23 17:53:56 +0000275 // Generate built-in symbol table.
276 if (!InitBuiltInSymbolTable(resources))
277 return false;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000278 InitExtensionBehavior(resources, extensionBehavior);
shannon.woods%transgaming.com@gtempaccount.comcbb6b6a2013-04-13 03:27:47 +0000279 fragmentPrecisionHigh = resources.FragmentPrecisionHigh == 1;
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000280
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000281 arrayBoundsClamper.SetClampingStrategy(resources.ArrayIndexClampingStrategy);
282 clampingStrategy = resources.ArrayIndexClampingStrategy;
283
daniel@transgaming.comc23f4612012-11-28 19:42:57 +0000284 hashFunction = resources.HashFunction;
285
alokp@chromium.org07620a52010-09-23 17:53:56 +0000286 return true;
287}
288
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100289TIntermBlock *TCompiler::compileTreeForTesting(const char *const shaderStrings[],
290 size_t numStrings,
291 ShCompileOptions compileOptions)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000292{
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200293 return compileTreeImpl(shaderStrings, numStrings, compileOptions);
294}
295
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100296TIntermBlock *TCompiler::compileTreeImpl(const char *const shaderStrings[],
297 size_t numStrings,
298 const ShCompileOptions compileOptions)
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200299{
alokp@chromium.org07620a52010-09-23 17:53:56 +0000300 clearResults();
301
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200302 ASSERT(numStrings > 0);
303 ASSERT(GetGlobalPoolAllocator());
alokp@chromium.org07620a52010-09-23 17:53:56 +0000304
David Yen0fbd1282015-02-02 14:46:09 -0800305 // Reset the extension behavior for each compilation unit.
306 ResetExtensionBehavior(extensionBehavior);
307
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000308 // First string is path of source file if flag is set. The actual source follows.
shannon.woods@transgaming.comd64b3da2013-02-28 23:19:26 +0000309 size_t firstSource = 0;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000310 if (compileOptions & SH_SOURCE_PATH)
311 {
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200312 mSourcePath = shaderStrings[0];
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000313 ++firstSource;
314 }
315
Olli Etuahof119a262016-08-19 15:54:22 +0300316 TParseContext parseContext(symbolTable, extensionBehavior, shaderType, shaderSpec,
Olli Etuaho77ba4082016-12-16 12:01:18 +0000317 compileOptions, true, &mDiagnostics, getResources());
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200318
Olli Etuahoa6996682015-10-12 14:32:30 +0300319 parseContext.setFragmentPrecisionHighOnESSL1(fragmentPrecisionHigh);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000320
321 // We preserve symbols at the built-in level from compile-to-compile.
322 // Start pushing the user-defined symbols at global level.
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400323 TScopedSymbolTableLevel scopedSymbolLevel(&symbolTable);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000324
325 // Parse shader.
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500326 bool success = (PaParseStrings(numStrings - firstSource, &shaderStrings[firstSource], nullptr,
327 &parseContext) == 0) &&
328 (parseContext.getTreeRoot() != nullptr);
shannon.woods%transgaming.com@gtempaccount.com0bbed382013-04-13 03:38:07 +0000329
shannon.woods%transgaming.com@gtempaccount.com5524db02013-04-13 03:38:16 +0000330 shaderVersion = parseContext.getShaderVersion();
Zhenyao Modb9b40b2014-10-29 15:00:04 -0700331 if (success && MapSpecToShaderVersion(shaderSpec) < shaderVersion)
332 {
Olli Etuaho77ba4082016-12-16 12:01:18 +0000333 mDiagnostics.globalError("unsupported shader version");
Zhenyao Modb9b40b2014-10-29 15:00:04 -0700334 success = false;
335 }
shannon.woods%transgaming.com@gtempaccount.com0bbed382013-04-13 03:38:07 +0000336
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100337 TIntermBlock *root = nullptr;
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200338
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800339 if (success)
340 {
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700341 mPragma = parseContext.pragma();
Kenneth Russell8bad46d2016-07-01 19:52:52 -0700342 symbolTable.setGlobalInvariant(mPragma.stdgl.invariantAll);
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700343
Martin Radev802abe02016-08-04 17:48:32 +0300344 mComputeShaderLocalSizeDeclared = parseContext.isComputeShaderLocalSizeDeclared();
345 mComputeShaderLocalSize = parseContext.getComputeShaderLocalSize();
346
Olli Etuaho09b04a22016-12-15 13:30:26 +0000347 mNumViews = parseContext.getNumViews();
348
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400349 root = parseContext.getTreeRoot();
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000350
Olli Etuahoa6996682015-10-12 14:32:30 +0300351 // Highp might have been auto-enabled based on shader version
352 fragmentPrecisionHigh = parseContext.getFragmentPrecisionHigh();
353
Olli Etuaho09b04a22016-12-15 13:30:26 +0000354 if (success && (IsWebGLBasedSpec(shaderSpec) &&
355 IsExtensionEnabled(extensionBehavior, "GL_OVR_multiview") &&
356 IsExtensionEnabled(extensionBehavior, "GL_OVR_multiview2")))
357 {
358 // Can't enable both extensions at the same time.
359 mDiagnostics.globalError("Can't enable both OVR_multiview and OVR_multiview2");
360 success = false;
361 }
362
Jamie Madill6654bc92014-03-26 14:01:57 -0400363 // Disallow expressions deemed too complex.
364 if (success && (compileOptions & SH_LIMIT_EXPRESSION_COMPLEXITY))
365 success = limitExpressionComplexity(root);
366
Corentin Wallez71d147f2015-02-11 11:15:24 -0800367 // Create the function DAG and check there is no recursion
zmo@google.comb1762df2011-07-30 02:04:23 +0000368 if (success)
Corentin Wallez71d147f2015-02-11 11:15:24 -0800369 success = initCallDag(root);
370
371 if (success && (compileOptions & SH_LIMIT_CALL_STACK_DEPTH))
372 success = checkCallDepth();
373
374 // Checks which functions are used and if "main" exists
375 if (success)
376 {
377 functionMetadata.clear();
378 functionMetadata.resize(mCallDag.size());
379 success = tagUsedFunctions();
380 }
zmo@google.comb1762df2011-07-30 02:04:23 +0000381
Corentin Walleza094a8a2015-04-07 11:53:06 -0700382 if (success && !(compileOptions & SH_DONT_PRUNE_UNUSED_FUNCTIONS))
383 success = pruneUnusedFunctions(root);
384
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500385 // Prune empty declarations to work around driver bugs and to keep declaration output
386 // simple.
Olli Etuahoc6833112015-04-22 15:15:54 +0300387 if (success)
388 PruneEmptyDeclarations(root);
389
Andrei Volykhin73fa4b82017-05-05 18:45:06 +0300390 if (success && shaderVersion >= 300 && shaderType == GL_FRAGMENT_SHADER)
Olli Etuaho12b0b392017-05-30 13:22:31 +0300391 {
392 success = ValidateOutputs(root, getExtensionBehavior(), compileResources.MaxDrawBuffers,
393 &mDiagnostics);
394 }
Jamie Madill05a80ce2013-06-20 11:55:49 -0400395
Olli Etuaho5d91dda2015-06-18 15:47:46 +0300396 if (success && shouldRunLoopAndIndexingValidation(compileOptions))
Olli Etuaho9ec79392017-03-31 23:04:23 +0300397 success =
Olli Etuahoa5e693a2017-07-13 16:07:26 +0300398 ValidateLimitations(root, shaderType, &symbolTable, shaderVersion, &mDiagnostics);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000399
Olli Etuaho09b04a22016-12-15 13:30:26 +0000400 bool multiview2 = IsExtensionEnabled(extensionBehavior, "GL_OVR_multiview2");
401 if (success && compileResources.OVR_multiview && IsWebGLBasedSpec(shaderSpec) &&
402 (IsExtensionEnabled(extensionBehavior, "GL_OVR_multiview") || multiview2))
Olli Etuaho01d0ad02017-01-22 14:51:23 -0800403 success = ValidateMultiviewWebGL(root, shaderType, symbolTable, shaderVersion,
404 multiview2, &mDiagnostics);
Olli Etuaho09b04a22016-12-15 13:30:26 +0000405
Jamie Madilld5696192016-10-06 11:09:24 -0400406 // Fail compilation if precision emulation not supported.
407 if (success && getResources().WEBGL_debug_shader_precision &&
408 getPragma().debugShaderPrecision)
409 {
410 if (!EmulatePrecision::SupportedInLanguage(outputType))
411 {
Olli Etuaho77ba4082016-12-16 12:01:18 +0000412 mDiagnostics.globalError("Precision emulation not supported for this output type.");
Jamie Madilld5696192016-10-06 11:09:24 -0400413 success = false;
414 }
415 }
416
zmo@google.com32e97312011-08-24 01:03:11 +0000417 // Built-in function emulation needs to happen after validateLimitations pass.
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200418 if (success)
419 {
Jamie Madill438dbcf2016-06-17 14:20:05 -0400420 // TODO(jmadill): Remove global pool allocator.
421 GetGlobalPoolAllocator()->lock();
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200422 initBuiltInFunctionEmulator(&builtInFunctionEmulator, compileOptions);
Jamie Madill438dbcf2016-06-17 14:20:05 -0400423 GetGlobalPoolAllocator()->unlock();
Olli Etuahodfa75e82017-01-23 09:43:06 -0800424 builtInFunctionEmulator.markBuiltInFunctionsForEmulation(root);
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200425 }
zmo@google.com32e97312011-08-24 01:03:11 +0000426
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000427 // Clamping uniform array bounds needs to happen after validateLimitations pass.
428 if (success && (compileOptions & SH_CLAMP_INDIRECT_ARRAY_BOUNDS))
429 arrayBoundsClamper.MarkIndirectArrayBoundsForClamping(root);
430
Martin Radev69056a12017-05-18 11:14:50 +0300431 if (success && (compileOptions & SH_INITIALIZE_BUILTINS_FOR_INSTANCED_MULTIVIEW) &&
Martin Radev7ef89a42017-07-05 14:23:06 +0300432 parseContext.isMultiviewExtensionEnabled() && getShaderType() != GL_COMPUTE_SHADER)
Martin Radev69056a12017-05-18 11:14:50 +0300433 {
Martin Radevc39a19a2017-07-07 18:52:09 +0300434 DeclareAndInitBuiltinsForInstancedMultiview(root, mNumViews, shaderType, compileOptions,
Olli Etuahoa5e693a2017-07-13 16:07:26 +0300435 outputType, &symbolTable);
Martin Radev69056a12017-05-18 11:14:50 +0300436 }
437
Corentin Wallezd4b50542015-09-28 12:19:26 -0700438 // This pass might emit short circuits so keep it before the short circuit unfolding
439 if (success && (compileOptions & SH_REWRITE_DO_WHILE_LOOPS))
Olli Etuahoa5e693a2017-07-13 16:07:26 +0300440 RewriteDoWhile(root, &symbolTable);
Corentin Wallezd4b50542015-09-28 12:19:26 -0700441
Qiankun Miao09cfac62016-09-06 17:25:16 +0800442 if (success && (compileOptions & SH_ADD_AND_TRUE_TO_LOOP_CONDITION))
443 sh::AddAndTrueToLoopCondition(root);
444
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800445 if (success && (compileOptions & SH_UNFOLD_SHORT_CIRCUIT))
446 {
Zhenyao Mo7cab38b2013-10-15 12:59:30 -0700447 UnfoldShortCircuitAST unfoldShortCircuit;
448 root->traverse(&unfoldShortCircuit);
449 unfoldShortCircuit.updateTree();
450 }
451
Olli Etuaho5c407bb2015-06-01 12:20:39 +0300452 if (success && (compileOptions & SH_REMOVE_POW_WITH_CONSTANT_EXPONENT))
453 {
454 RemovePow(root);
455 }
456
Olli Etuaho4dfe8092015-08-21 17:44:35 +0300457 if (success && shouldCollectVariables(compileOptions))
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800458 {
Olli Etuaho19515012017-06-26 18:00:17 +0300459 ASSERT(!variablesCollected);
460 CollectVariables(root, &attributes, &outputVariables, &uniforms, &varyings,
Olli Etuahoa5e693a2017-07-13 16:07:26 +0300461 &interfaceBlocks, hashFunction, &symbolTable, shaderVersion,
Olli Etuaho19515012017-06-26 18:00:17 +0300462 extensionBehavior);
463 variablesCollected = true;
Qin Jiajia7835b522016-10-08 11:20:17 +0800464 if (compileOptions & SH_USE_UNUSED_STANDARD_SHARED_BLOCKS)
465 {
466 useAllMembersInUnusedStandardAndSharedBlocks(root);
467 }
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800468 if (compileOptions & SH_ENFORCE_PACKING_RESTRICTIONS)
469 {
Olli Etuaho19515012017-06-26 18:00:17 +0300470 std::vector<sh::ShaderVariable> expandedUniforms;
471 sh::ExpandUniforms(uniforms, &expandedUniforms);
472 VariablePacker packer;
473 // Returns true if, after applying the packing rules in the GLSL ES 1.00.17 spec
474 // Appendix A, section 7, the shader does not use too many uniforms.
475 success =
476 packer.CheckVariablesWithinPackingLimits(maxUniformVectors, expandedUniforms);
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800477 if (!success)
478 {
Olli Etuaho77ba4082016-12-16 12:01:18 +0000479 mDiagnostics.globalError("too many uniforms");
gman@chromium.org8d804792012-10-17 21:33:48 +0000480 }
481 }
Zhenyao Mo72111912016-07-20 17:45:56 -0700482 if (success && (compileOptions & SH_INIT_OUTPUT_VARIABLES))
483 {
Olli Etuaho27776e32016-07-22 14:00:56 +0300484 initializeOutputVariables(root);
Zhenyao Mo72111912016-07-20 17:45:56 -0700485 }
gman@chromium.org8d804792012-10-17 21:33:48 +0000486 }
zmo@google.comfd747b82011-04-23 01:30:07 +0000487
Olli Etuahob12040c2017-06-27 14:20:45 +0300488 // gl_Position is always written in compatibility output mode.
489 // It may have been already initialized among other output variables, in that case we don't
490 // need to initialize it twice.
491 if (success && shaderType == GL_VERTEX_SHADER && !mGLPositionInitialized &&
492 ((compileOptions & SH_INIT_GL_POSITION) ||
493 (outputType == SH_GLSL_COMPATIBILITY_OUTPUT)))
494 {
495 initializeGLPosition(root);
496 mGLPositionInitialized = true;
497 }
498
Yuly Novikov817232e2017-02-22 18:36:10 -0500499 // Removing invariant declarations must be done after collecting variables.
500 // Otherwise, built-in invariant declarations don't apply.
501 if (success && RemoveInvariant(shaderType, shaderVersion, outputType, compileOptions))
502 sh::RemoveInvariantDeclaration(root);
503
Zhenyao Mocd68fe72014-07-11 10:45:44 -0700504 if (success && (compileOptions & SH_SCALARIZE_VEC_AND_MAT_CONSTRUCTOR_ARGS))
505 {
Olli Etuahob990b552016-10-27 12:29:17 +0100506 ScalarizeVecAndMatConstructorArgs(root, shaderType, fragmentPrecisionHigh,
Olli Etuahoa5e693a2017-07-13 16:07:26 +0300507 &symbolTable);
Zhenyao Mocd68fe72014-07-11 10:45:44 -0700508 }
509
Zhenyao Moe740add2014-07-18 17:01:01 -0700510 if (success && (compileOptions & SH_REGENERATE_STRUCT_NAMES))
511 {
Olli Etuahoa5e693a2017-07-13 16:07:26 +0300512 RegenerateStructNames gen(&symbolTable, shaderVersion);
Zhenyao Moe740add2014-07-18 17:01:01 -0700513 root->traverse(&gen);
514 }
Olli Etuaho3d932d82016-04-12 11:10:30 +0300515
Zhenyao Mo4e94fea2016-08-09 14:31:37 -0700516 if (success && shaderType == GL_FRAGMENT_SHADER && shaderVersion == 100 &&
517 compileResources.EXT_draw_buffers && compileResources.MaxDrawBuffers > 1 &&
518 IsExtensionEnabled(extensionBehavior, "GL_EXT_draw_buffers"))
519 {
Olli Etuahodaaff1c2017-07-05 18:03:26 +0300520 EmulateGLFragColorBroadcast(root, compileResources.MaxDrawBuffers, &outputVariables,
Olli Etuahoa5e693a2017-07-13 16:07:26 +0300521 &symbolTable, shaderVersion);
Zhenyao Mo4e94fea2016-08-09 14:31:37 -0700522 }
523
Olli Etuaho3d932d82016-04-12 11:10:30 +0300524 if (success)
525 {
Olli Etuaho0ffc4412017-05-19 14:18:55 +0300526 DeferGlobalInitializers(root, needToInitializeGlobalsInAST());
Olli Etuaho3d932d82016-04-12 11:10:30 +0300527 }
Olli Etuaho9733cee2017-05-11 19:14:35 +0300528
529 if (success && (compileOptions & SH_INITIALIZE_UNINITIALIZED_LOCALS) && getOutputType())
530 {
531 // Initialize uninitialized local variables.
532 // In some cases initializing can generate extra statements in the parent block, such as
533 // when initializing nameless structs or initializing arrays in ESSL 1.00. In that case
534 // we need to first simplify loop conditions and separate declarations. If we don't
535 // follow the Appendix A limitations, loop init statements can declare arrays or
536 // nameless structs and have multiple declarations.
537
538 if (!shouldRunLoopAndIndexingValidation(compileOptions))
539 {
540 SimplifyLoopConditions(root,
541 IntermNodePatternMatcher::kMultiDeclaration |
542 IntermNodePatternMatcher::kArrayDeclaration |
543 IntermNodePatternMatcher::kNamelessStructDeclaration,
Olli Etuahoa5e693a2017-07-13 16:07:26 +0300544 &getSymbolTable(), getShaderVersion());
Olli Etuaho9733cee2017-05-11 19:14:35 +0300545 }
546 // We only really need to separate array declarations and nameless struct declarations,
547 // but it's simpler to just use the regular SeparateDeclarations.
548 SeparateDeclarations(root);
549 InitializeUninitializedLocals(root, getShaderVersion());
550 }
Olli Etuahoab918822017-07-14 17:03:42 +0300551
552 if (success && getShaderType() == GL_VERTEX_SHADER &&
553 (compileOptions & SH_CLAMP_POINT_SIZE))
554 {
555 ClampPointSize(root, compileResources.MaxPointSize, &getSymbolTable());
556 }
alokp@chromium.org07620a52010-09-23 17:53:56 +0000557 }
558
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200559 if (success)
560 return root;
561
Yunchao Hef81ce4a2017-04-24 10:49:17 +0800562 return nullptr;
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200563}
564
Qiankun Miao7ebb97f2016-09-08 18:01:50 +0800565bool TCompiler::compile(const char *const shaderStrings[],
566 size_t numStrings,
567 ShCompileOptions compileOptionsIn)
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200568{
Corentin Wallez28b65282016-06-16 07:24:50 -0700569#if defined(ANGLE_ENABLE_FUZZER_CORPUS_OUTPUT)
570 DumpFuzzerCase(shaderStrings, numStrings, shaderType, shaderSpec, outputType, compileOptionsIn);
571#endif // defined(ANGLE_ENABLE_FUZZER_CORPUS_OUTPUT)
572
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200573 if (numStrings == 0)
574 return true;
575
Qiankun Miao7ebb97f2016-09-08 18:01:50 +0800576 ShCompileOptions compileOptions = compileOptionsIn;
Kenneth Russellbccc65d2016-07-19 16:48:43 -0700577
578 // Apply key workarounds.
579 if (shouldFlattenPragmaStdglInvariantAll())
580 {
581 // This should be harmless to do in all cases, but for the moment, do it only conditionally.
582 compileOptions |= SH_FLATTEN_PRAGMA_STDGL_INVARIANT_ALL;
583 }
584
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200585 TScopedPoolAllocator scopedAlloc(&allocator);
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100586 TIntermBlock *root = compileTreeImpl(shaderStrings, numStrings, compileOptions);
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200587
588 if (root)
589 {
590 if (compileOptions & SH_INTERMEDIATE_TREE)
Olli Etuahocccf2b02017-07-05 14:50:54 +0300591 OutputTree(root, infoSink.info);
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200592
593 if (compileOptions & SH_OBJECT_CODE)
594 translate(root, compileOptions);
595
596 // The IntermNode tree doesn't need to be deleted here, since the
597 // memory will be freed in a big chunk by the PoolAllocator.
598 return true;
599 }
600 return false;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000601}
602
Nicolas Capens49a88872013-06-20 09:54:03 -0400603bool TCompiler::InitBuiltInSymbolTable(const ShBuiltInResources &resources)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000604{
Olli Etuaho28cb0362016-11-22 15:42:37 +0000605 if (resources.MaxDrawBuffers < 1)
606 {
607 return false;
608 }
609 if (resources.EXT_blend_func_extended && resources.MaxDualSourceDrawBuffers < 1)
610 {
611 return false;
612 }
613
shannon.woods%transgaming.com@gtempaccount.com18b4c4b2013-04-13 03:31:40 +0000614 compileResources = resources;
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400615 setResourceString();
shannonwoods@chromium.org2ac0be92013-05-30 00:02:27 +0000616
Nicolas Capens49a88872013-06-20 09:54:03 -0400617 assert(symbolTable.isEmpty());
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500618 symbolTable.push(); // COMMON_BUILTINS
619 symbolTable.push(); // ESSL1_BUILTINS
620 symbolTable.push(); // ESSL3_BUILTINS
621 symbolTable.push(); // ESSL3_1_BUILTINS
shannonwoods@chromium.org2ac0be92013-05-30 00:02:27 +0000622
Jamie Madillacb4b812016-11-07 13:50:29 -0500623 switch (shaderType)
Nicolas Capens49a88872013-06-20 09:54:03 -0400624 {
Jamie Madillacb4b812016-11-07 13:50:29 -0500625 case GL_FRAGMENT_SHADER:
Olli Etuahocce89652017-06-19 16:04:09 +0300626 symbolTable.setDefaultPrecision(EbtInt, EbpMedium);
Jamie Madillacb4b812016-11-07 13:50:29 -0500627 break;
628 case GL_VERTEX_SHADER:
Olli Etuahocce89652017-06-19 16:04:09 +0300629 symbolTable.setDefaultPrecision(EbtInt, EbpHigh);
630 symbolTable.setDefaultPrecision(EbtFloat, EbpHigh);
Jamie Madillacb4b812016-11-07 13:50:29 -0500631 break;
632 case GL_COMPUTE_SHADER:
Olli Etuahocce89652017-06-19 16:04:09 +0300633 symbolTable.setDefaultPrecision(EbtInt, EbpHigh);
634 symbolTable.setDefaultPrecision(EbtFloat, EbpHigh);
Jamie Madillacb4b812016-11-07 13:50:29 -0500635 break;
636 default:
637 assert(false && "Language not supported");
Nicolas Capens49a88872013-06-20 09:54:03 -0400638 }
Olli Etuaho183d7e22015-11-20 15:59:09 +0200639 // Set defaults for sampler types that have default precision, even those that are
Zhenyao Moa5a1dfc2013-09-23 14:57:03 -0400640 // only available if an extension exists.
Olli Etuaho183d7e22015-11-20 15:59:09 +0200641 // New sampler types in ESSL3 don't have default precision. ESSL1 types do.
642 initSamplerDefaultPrecision(EbtSampler2D);
643 initSamplerDefaultPrecision(EbtSamplerCube);
644 // SamplerExternalOES is specified in the extension to have default precision.
645 initSamplerDefaultPrecision(EbtSamplerExternalOES);
Andrei Volykhina5527072017-03-22 16:46:30 +0300646 // SamplerExternal2DY2YEXT is specified in the extension to have default precision.
647 initSamplerDefaultPrecision(EbtSamplerExternal2DY2YEXT);
Olli Etuaho183d7e22015-11-20 15:59:09 +0200648 // It isn't specified whether Sampler2DRect has default precision.
649 initSamplerDefaultPrecision(EbtSampler2DRect);
Nicolas Capens49a88872013-06-20 09:54:03 -0400650
Olli Etuahocce89652017-06-19 16:04:09 +0300651 symbolTable.setDefaultPrecision(EbtAtomicCounter, EbpHigh);
jchen104cdac9e2017-05-08 11:01:20 +0800652
Jamie Madill1b452142013-07-12 14:51:11 -0400653 InsertBuiltInFunctions(shaderType, shaderSpec, resources, symbolTable);
Nicolas Capens49a88872013-06-20 09:54:03 -0400654
655 IdentifyBuiltIns(shaderType, shaderSpec, resources, symbolTable);
656
657 return true;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000658}
659
Olli Etuaho183d7e22015-11-20 15:59:09 +0200660void TCompiler::initSamplerDefaultPrecision(TBasicType samplerType)
661{
662 ASSERT(samplerType > EbtGuardSamplerBegin && samplerType < EbtGuardSamplerEnd);
Olli Etuahocce89652017-06-19 16:04:09 +0300663 symbolTable.setDefaultPrecision(samplerType, EbpLow);
Olli Etuaho183d7e22015-11-20 15:59:09 +0200664}
665
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400666void TCompiler::setResourceString()
667{
668 std::ostringstream strstream;
Geoff Langb66a9092016-05-16 15:59:14 -0400669
670 // clang-format off
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400671 strstream << ":MaxVertexAttribs:" << compileResources.MaxVertexAttribs
Jamie Madillacb4b812016-11-07 13:50:29 -0500672 << ":MaxVertexUniformVectors:" << compileResources.MaxVertexUniformVectors
673 << ":MaxVaryingVectors:" << compileResources.MaxVaryingVectors
674 << ":MaxVertexTextureImageUnits:" << compileResources.MaxVertexTextureImageUnits
675 << ":MaxCombinedTextureImageUnits:" << compileResources.MaxCombinedTextureImageUnits
676 << ":MaxTextureImageUnits:" << compileResources.MaxTextureImageUnits
677 << ":MaxFragmentUniformVectors:" << compileResources.MaxFragmentUniformVectors
678 << ":MaxDrawBuffers:" << compileResources.MaxDrawBuffers
679 << ":OES_standard_derivatives:" << compileResources.OES_standard_derivatives
680 << ":OES_EGL_image_external:" << compileResources.OES_EGL_image_external
681 << ":OES_EGL_image_external_essl3:" << compileResources.OES_EGL_image_external_essl3
682 << ":NV_EGL_stream_consumer_external:" << compileResources.NV_EGL_stream_consumer_external
683 << ":ARB_texture_rectangle:" << compileResources.ARB_texture_rectangle
684 << ":EXT_draw_buffers:" << compileResources.EXT_draw_buffers
685 << ":FragmentPrecisionHigh:" << compileResources.FragmentPrecisionHigh
686 << ":MaxExpressionComplexity:" << compileResources.MaxExpressionComplexity
687 << ":MaxCallStackDepth:" << compileResources.MaxCallStackDepth
688 << ":MaxFunctionParameters:" << compileResources.MaxFunctionParameters
689 << ":EXT_blend_func_extended:" << compileResources.EXT_blend_func_extended
690 << ":EXT_frag_depth:" << compileResources.EXT_frag_depth
691 << ":EXT_shader_texture_lod:" << compileResources.EXT_shader_texture_lod
692 << ":EXT_shader_framebuffer_fetch:" << compileResources.EXT_shader_framebuffer_fetch
693 << ":NV_shader_framebuffer_fetch:" << compileResources.NV_shader_framebuffer_fetch
694 << ":ARM_shader_framebuffer_fetch:" << compileResources.ARM_shader_framebuffer_fetch
Martin Radev318f9aa2017-05-17 17:47:28 +0300695 << ":OVR_multiview:" << compileResources.OVR_multiview
Andrei Volykhina5527072017-03-22 16:46:30 +0300696 << ":EXT_YUV_target:" << compileResources.EXT_YUV_target
Jamie Madillacb4b812016-11-07 13:50:29 -0500697 << ":MaxVertexOutputVectors:" << compileResources.MaxVertexOutputVectors
698 << ":MaxFragmentInputVectors:" << compileResources.MaxFragmentInputVectors
699 << ":MinProgramTexelOffset:" << compileResources.MinProgramTexelOffset
700 << ":MaxProgramTexelOffset:" << compileResources.MaxProgramTexelOffset
701 << ":MaxDualSourceDrawBuffers:" << compileResources.MaxDualSourceDrawBuffers
Martin Radev318f9aa2017-05-17 17:47:28 +0300702 << ":MaxViewsOVR:" << compileResources.MaxViewsOVR
Jamie Madillacb4b812016-11-07 13:50:29 -0500703 << ":NV_draw_buffers:" << compileResources.NV_draw_buffers
704 << ":WEBGL_debug_shader_precision:" << compileResources.WEBGL_debug_shader_precision
705 << ":MaxImageUnits:" << compileResources.MaxImageUnits
706 << ":MaxVertexImageUniforms:" << compileResources.MaxVertexImageUniforms
707 << ":MaxFragmentImageUniforms:" << compileResources.MaxFragmentImageUniforms
708 << ":MaxComputeImageUniforms:" << compileResources.MaxComputeImageUniforms
709 << ":MaxCombinedImageUniforms:" << compileResources.MaxCombinedImageUniforms
710 << ":MaxCombinedShaderOutputResources:" << compileResources.MaxCombinedShaderOutputResources
711 << ":MaxComputeWorkGroupCountX:" << compileResources.MaxComputeWorkGroupCount[0]
712 << ":MaxComputeWorkGroupCountY:" << compileResources.MaxComputeWorkGroupCount[1]
713 << ":MaxComputeWorkGroupCountZ:" << compileResources.MaxComputeWorkGroupCount[2]
714 << ":MaxComputeWorkGroupSizeX:" << compileResources.MaxComputeWorkGroupSize[0]
715 << ":MaxComputeWorkGroupSizeY:" << compileResources.MaxComputeWorkGroupSize[1]
716 << ":MaxComputeWorkGroupSizeZ:" << compileResources.MaxComputeWorkGroupSize[2]
717 << ":MaxComputeUniformComponents:" << compileResources.MaxComputeUniformComponents
718 << ":MaxComputeTextureImageUnits:" << compileResources.MaxComputeTextureImageUnits
719 << ":MaxComputeAtomicCounters:" << compileResources.MaxComputeAtomicCounters
720 << ":MaxComputeAtomicCounterBuffers:" << compileResources.MaxComputeAtomicCounterBuffers
721 << ":MaxVertexAtomicCounters:" << compileResources.MaxVertexAtomicCounters
722 << ":MaxFragmentAtomicCounters:" << compileResources.MaxFragmentAtomicCounters
723 << ":MaxCombinedAtomicCounters:" << compileResources.MaxCombinedAtomicCounters
724 << ":MaxAtomicCounterBindings:" << compileResources.MaxAtomicCounterBindings
725 << ":MaxVertexAtomicCounterBuffers:" << compileResources.MaxVertexAtomicCounterBuffers
726 << ":MaxFragmentAtomicCounterBuffers:" << compileResources.MaxFragmentAtomicCounterBuffers
727 << ":MaxCombinedAtomicCounterBuffers:" << compileResources.MaxCombinedAtomicCounterBuffers
728 << ":MaxAtomicCounterBufferSize:" << compileResources.MaxAtomicCounterBufferSize;
Geoff Langb66a9092016-05-16 15:59:14 -0400729 // clang-format on
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400730
731 builtInResourcesString = strstream.str();
732}
733
alokp@chromium.org07620a52010-09-23 17:53:56 +0000734void TCompiler::clearResults()
735{
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000736 arrayBoundsClamper.Cleanup();
alokp@chromium.org07620a52010-09-23 17:53:56 +0000737 infoSink.info.erase();
738 infoSink.obj.erase();
739 infoSink.debug.erase();
Olli Etuaho77ba4082016-12-16 12:01:18 +0000740 mDiagnostics.resetErrorCount();
alokp@chromium.org07620a52010-09-23 17:53:56 +0000741
Jamie Madilled27c722014-07-02 15:31:23 -0400742 attributes.clear();
743 outputVariables.clear();
alokp@chromium.org07620a52010-09-23 17:53:56 +0000744 uniforms.clear();
Zhenyao Mod2d340b2013-09-23 14:57:05 -0400745 varyings.clear();
Jamie Madilled27c722014-07-02 15:31:23 -0400746 interfaceBlocks.clear();
Kenneth Russellbccc65d2016-07-19 16:48:43 -0700747 variablesCollected = false;
Olli Etuahob12040c2017-06-27 14:20:45 +0300748 mGLPositionInitialized = false;
zmo@google.coma3b4ab42011-09-16 00:53:26 +0000749
Olli Etuaho09b04a22016-12-15 13:30:26 +0000750 mNumViews = -1;
751
Olli Etuahodfa75e82017-01-23 09:43:06 -0800752 builtInFunctionEmulator.cleanup();
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +0000753
754 nameMap.clear();
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200755
Yunchao Hed7297bf2017-04-19 15:27:10 +0800756 mSourcePath = nullptr;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000757}
758
Corentin Wallez71d147f2015-02-11 11:15:24 -0800759bool TCompiler::initCallDag(TIntermNode *root)
zmo@google.comb1762df2011-07-30 02:04:23 +0000760{
Corentin Wallez71d147f2015-02-11 11:15:24 -0800761 mCallDag.clear();
762
Olli Etuaho77ba4082016-12-16 12:01:18 +0000763 switch (mCallDag.init(root, &mDiagnostics))
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800764 {
Jamie Madillacb4b812016-11-07 13:50:29 -0500765 case CallDAG::INITDAG_SUCCESS:
766 return true;
767 case CallDAG::INITDAG_RECURSION:
Jamie Madillacb4b812016-11-07 13:50:29 -0500768 case CallDAG::INITDAG_UNDEFINED:
Olli Etuaho77ba4082016-12-16 12:01:18 +0000769 // Error message has already been written out.
770 ASSERT(mDiagnostics.numErrors() > 0);
Jamie Madillacb4b812016-11-07 13:50:29 -0500771 return false;
Corentin Wallez71d147f2015-02-11 11:15:24 -0800772 }
773
774 UNREACHABLE();
775 return true;
776}
777
778bool TCompiler::checkCallDepth()
779{
780 std::vector<int> depths(mCallDag.size());
781
782 for (size_t i = 0; i < mCallDag.size(); i++)
783 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500784 int depth = 0;
Corentin Wallez71d147f2015-02-11 11:15:24 -0800785 auto &record = mCallDag.getRecordFromIndex(i);
786
787 for (auto &calleeIndex : record.callees)
788 {
789 depth = std::max(depth, depths[calleeIndex] + 1);
790 }
791
792 depths[i] = depth;
793
794 if (depth >= maxCallStackDepth)
795 {
796 // Trace back the function chain to have a meaningful info log.
Olli Etuaho77ba4082016-12-16 12:01:18 +0000797 std::stringstream errorStream;
798 errorStream << "Call stack too deep (larger than " << maxCallStackDepth
799 << ") with the following call chain: " << record.name;
Corentin Wallez71d147f2015-02-11 11:15:24 -0800800
Cooper Partin4d61f7e2015-08-12 10:56:50 -0700801 int currentFunction = static_cast<int>(i);
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500802 int currentDepth = depth;
Corentin Wallez71d147f2015-02-11 11:15:24 -0800803
804 while (currentFunction != -1)
805 {
Olli Etuaho77ba4082016-12-16 12:01:18 +0000806 errorStream << " -> " << mCallDag.getRecordFromIndex(currentFunction).name;
Corentin Wallez71d147f2015-02-11 11:15:24 -0800807
808 int nextFunction = -1;
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500809 for (auto &calleeIndex : mCallDag.getRecordFromIndex(currentFunction).callees)
Corentin Wallez71d147f2015-02-11 11:15:24 -0800810 {
811 if (depths[calleeIndex] == currentDepth - 1)
812 {
813 currentDepth--;
814 nextFunction = calleeIndex;
815 }
816 }
817
818 currentFunction = nextFunction;
819 }
820
Olli Etuaho77ba4082016-12-16 12:01:18 +0000821 std::string errorStr = errorStream.str();
822 mDiagnostics.globalError(errorStr.c_str());
823
Corentin Wallez71d147f2015-02-11 11:15:24 -0800824 return false;
825 }
826 }
827
828 return true;
829}
830
831bool TCompiler::tagUsedFunctions()
832{
833 // Search from main, starting from the end of the DAG as it usually is the root.
Cooper Partin4d61f7e2015-08-12 10:56:50 -0700834 for (size_t i = mCallDag.size(); i-- > 0;)
Corentin Wallez71d147f2015-02-11 11:15:24 -0800835 {
Olli Etuahoec9232b2017-03-27 17:01:37 +0300836 if (mCallDag.getRecordFromIndex(i).name == "main")
Corentin Wallez71d147f2015-02-11 11:15:24 -0800837 {
838 internalTagUsedFunction(i);
839 return true;
840 }
841 }
842
Olli Etuaho77ba4082016-12-16 12:01:18 +0000843 mDiagnostics.globalError("Missing main()");
Corentin Wallez71d147f2015-02-11 11:15:24 -0800844 return false;
845}
846
847void TCompiler::internalTagUsedFunction(size_t index)
848{
849 if (functionMetadata[index].used)
850 {
851 return;
852 }
853
854 functionMetadata[index].used = true;
855
856 for (int calleeIndex : mCallDag.getRecordFromIndex(index).callees)
857 {
858 internalTagUsedFunction(calleeIndex);
zmo@google.comb1762df2011-07-30 02:04:23 +0000859 }
860}
861
Corentin Walleza094a8a2015-04-07 11:53:06 -0700862// A predicate for the stl that returns if a top-level node is unused
863class TCompiler::UnusedPredicate
864{
865 public:
866 UnusedPredicate(const CallDAG *callDag, const std::vector<FunctionMetadata> *metadatas)
Jamie Madillacb4b812016-11-07 13:50:29 -0500867 : mCallDag(callDag), mMetadatas(metadatas)
Corentin Walleza094a8a2015-04-07 11:53:06 -0700868 {
869 }
870
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500871 bool operator()(TIntermNode *node)
Corentin Walleza094a8a2015-04-07 11:53:06 -0700872 {
Olli Etuaho16c745a2017-01-16 17:02:27 +0000873 const TIntermFunctionPrototype *asFunctionPrototype = node->getAsFunctionPrototypeNode();
874 const TIntermFunctionDefinition *asFunctionDefinition = node->getAsFunctionDefinition();
Corentin Walleza094a8a2015-04-07 11:53:06 -0700875
Olli Etuaho336b1472016-10-05 16:37:55 +0100876 const TFunctionSymbolInfo *functionInfo = nullptr;
877
Olli Etuaho16c745a2017-01-16 17:02:27 +0000878 if (asFunctionDefinition)
Olli Etuaho336b1472016-10-05 16:37:55 +0100879 {
Olli Etuaho16c745a2017-01-16 17:02:27 +0000880 functionInfo = asFunctionDefinition->getFunctionSymbolInfo();
Olli Etuaho336b1472016-10-05 16:37:55 +0100881 }
Olli Etuaho16c745a2017-01-16 17:02:27 +0000882 else if (asFunctionPrototype)
Olli Etuaho336b1472016-10-05 16:37:55 +0100883 {
Olli Etuaho16c745a2017-01-16 17:02:27 +0000884 functionInfo = asFunctionPrototype->getFunctionSymbolInfo();
Olli Etuaho336b1472016-10-05 16:37:55 +0100885 }
886 if (functionInfo == nullptr)
Corentin Walleza094a8a2015-04-07 11:53:06 -0700887 {
888 return false;
889 }
890
Olli Etuaho336b1472016-10-05 16:37:55 +0100891 size_t callDagIndex = mCallDag->findIndex(functionInfo);
Corentin Walleza094a8a2015-04-07 11:53:06 -0700892 if (callDagIndex == CallDAG::InvalidIndex)
893 {
894 // This happens only for unimplemented prototypes which are thus unused
Olli Etuaho16c745a2017-01-16 17:02:27 +0000895 ASSERT(asFunctionPrototype);
Corentin Walleza094a8a2015-04-07 11:53:06 -0700896 return true;
897 }
898
899 ASSERT(callDagIndex < mMetadatas->size());
900 return !(*mMetadatas)[callDagIndex].used;
901 }
902
903 private:
904 const CallDAG *mCallDag;
905 const std::vector<FunctionMetadata> *mMetadatas;
906};
907
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100908bool TCompiler::pruneUnusedFunctions(TIntermBlock *root)
Corentin Walleza094a8a2015-04-07 11:53:06 -0700909{
Corentin Walleza094a8a2015-04-07 11:53:06 -0700910 UnusedPredicate isUnused(&mCallDag, &functionMetadata);
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100911 TIntermSequence *sequence = root->getSequence();
Corentin Wallezb081e782015-07-20 05:40:04 -0700912
913 if (!sequence->empty())
914 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500915 sequence->erase(std::remove_if(sequence->begin(), sequence->end(), isUnused),
916 sequence->end());
Corentin Wallezb081e782015-07-20 05:40:04 -0700917 }
Corentin Walleza094a8a2015-04-07 11:53:06 -0700918
919 return true;
920}
921
Olli Etuahob4279202017-05-18 15:08:24 +0300922bool TCompiler::limitExpressionComplexity(TIntermBlock *root)
Jamie Madilleb1a0102013-07-08 13:31:38 -0400923{
Olli Etuahocccf2b02017-07-05 14:50:54 +0300924 if (!IsASTDepthBelowLimit(root, maxExpressionComplexity))
Jamie Madill6654bc92014-03-26 14:01:57 -0400925 {
Olli Etuaho77ba4082016-12-16 12:01:18 +0000926 mDiagnostics.globalError("Expression too complex.");
Jamie Madill6654bc92014-03-26 14:01:57 -0400927 return false;
928 }
929
Olli Etuahob4279202017-05-18 15:08:24 +0300930 if (!ValidateMaxParameters(root, maxFunctionParameters))
Olli Etuaho19d1dc92016-03-08 17:18:46 +0200931 {
Olli Etuaho77ba4082016-12-16 12:01:18 +0000932 mDiagnostics.globalError("Function has too many parameters.");
Olli Etuaho19d1dc92016-03-08 17:18:46 +0200933 return false;
934 }
935
Jamie Madilleb1a0102013-07-08 13:31:38 -0400936 return true;
937}
938
Corentin Wallez1df16022016-10-27 08:16:56 -0400939bool TCompiler::shouldCollectVariables(ShCompileOptions compileOptions)
940{
941 return (compileOptions & SH_VARIABLES) != 0;
942}
943
944bool TCompiler::wereVariablesCollected() const
945{
946 return variablesCollected;
947}
948
Olli Etuaho9cbc07c2017-05-10 18:22:01 +0300949void TCompiler::initializeGLPosition(TIntermBlock *root)
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800950{
Zhenyao Mo72111912016-07-20 17:45:56 -0700951 InitVariableList list;
952 sh::ShaderVariable var(GL_FLOAT_VEC4, 0);
953 var.name = "gl_Position";
954 list.push_back(var);
Olli Etuahodaaff1c2017-07-05 18:03:26 +0300955 InitializeVariables(root, list, symbolTable, shaderVersion, extensionBehavior);
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800956}
957
Olli Etuaho9cbc07c2017-05-10 18:22:01 +0300958void TCompiler::useAllMembersInUnusedStandardAndSharedBlocks(TIntermBlock *root)
Qin Jiajia7835b522016-10-08 11:20:17 +0800959{
960 sh::InterfaceBlockList list;
961
962 for (auto block : interfaceBlocks)
963 {
964 if (!block.staticUse &&
965 (block.layout == sh::BLOCKLAYOUT_STANDARD || block.layout == sh::BLOCKLAYOUT_SHARED))
966 {
967 list.push_back(block);
968 }
969 }
970
Zhenyao Mod7490962016-11-09 15:49:51 -0800971 sh::UseInterfaceBlockFields(root, list, symbolTable);
Qin Jiajia7835b522016-10-08 11:20:17 +0800972}
973
Olli Etuaho9cbc07c2017-05-10 18:22:01 +0300974void TCompiler::initializeOutputVariables(TIntermBlock *root)
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800975{
Zhenyao Mo72111912016-07-20 17:45:56 -0700976 InitVariableList list;
977 if (shaderType == GL_VERTEX_SHADER)
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800978 {
Zhenyao Mo72111912016-07-20 17:45:56 -0700979 for (auto var : varyings)
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800980 {
Zhenyao Mof9312682016-07-22 12:51:31 -0700981 list.push_back(var);
Olli Etuahob12040c2017-06-27 14:20:45 +0300982 if (var.name == "gl_Position")
983 {
984 ASSERT(!mGLPositionInitialized);
985 mGLPositionInitialized = true;
986 }
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800987 }
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800988 }
Zhenyao Mo72111912016-07-20 17:45:56 -0700989 else
990 {
991 ASSERT(shaderType == GL_FRAGMENT_SHADER);
992 for (auto var : outputVariables)
993 {
994 list.push_back(var);
995 }
996 }
Olli Etuahodaaff1c2017-07-05 18:03:26 +0300997 InitializeVariables(root, list, symbolTable, shaderVersion, extensionBehavior);
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800998}
999
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001000const TExtensionBehavior &TCompiler::getExtensionBehavior() const
zmo@google.com5601ea02011-06-10 18:23:25 +00001001{
1002 return extensionBehavior;
1003}
zmo@google.com32e97312011-08-24 01:03:11 +00001004
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02001005const char *TCompiler::getSourcePath() const
1006{
1007 return mSourcePath;
1008}
1009
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001010const ShBuiltInResources &TCompiler::getResources() const
shannon.woods%transgaming.com@gtempaccount.com18b4c4b2013-04-13 03:31:40 +00001011{
1012 return compileResources;
1013}
1014
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001015const ArrayBoundsClamper &TCompiler::getArrayBoundsClamper() const
daniel@transgaming.com4167cc92013-01-11 04:11:53 +00001016{
1017 return arrayBoundsClamper;
1018}
1019
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +00001020ShArrayIndexClampingStrategy TCompiler::getArrayIndexClampingStrategy() const
1021{
1022 return clampingStrategy;
1023}
1024
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001025const BuiltInFunctionEmulator &TCompiler::getBuiltInFunctionEmulator() const
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +00001026{
1027 return builtInFunctionEmulator;
1028}
Zhenyao Mo94ac7b72014-10-15 18:22:08 -07001029
Qiankun Miao7ebb97f2016-09-08 18:01:50 +08001030void TCompiler::writePragma(ShCompileOptions compileOptions)
Zhenyao Mo94ac7b72014-10-15 18:22:08 -07001031{
Kenneth Russellbccc65d2016-07-19 16:48:43 -07001032 if (!(compileOptions & SH_FLATTEN_PRAGMA_STDGL_INVARIANT_ALL))
1033 {
1034 TInfoSinkBase &sink = infoSink.obj;
1035 if (mPragma.stdgl.invariantAll)
1036 sink << "#pragma STDGL invariant(all)\n";
1037 }
1038}
1039
1040bool TCompiler::isVaryingDefined(const char *varyingName)
1041{
1042 ASSERT(variablesCollected);
1043 for (size_t ii = 0; ii < varyings.size(); ++ii)
1044 {
1045 if (varyings[ii].name == varyingName)
1046 {
1047 return true;
1048 }
1049 }
1050
1051 return false;
Zhenyao Mo94ac7b72014-10-15 18:22:08 -07001052}
Jamie Madillacb4b812016-11-07 13:50:29 -05001053
1054} // namespace sh