blob: 6f9b7e1bea990c58d9b6ef19646a16fbe439f7cc [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"
Corentin Wallez71d147f2015-02-11 11:15:24 -080013#include "compiler/translator/CallDAG.h"
Olli Etuaho78ed6cd2017-08-09 16:19:00 +030014#include "compiler/translator/CollectVariables.h"
Geoff Lang17732822013-08-29 13:46:49 -040015#include "compiler/translator/Initialize.h"
Olli Etuahocccf2b02017-07-05 14:50:54 +030016#include "compiler/translator/IsASTDepthBelowLimit.h"
17#include "compiler/translator/OutputTree.h"
Jamie Madill6b9cb252013-10-17 10:45:47 -040018#include "compiler/translator/ParseContext.h"
Geoff Lang17732822013-08-29 13:46:49 -040019#include "compiler/translator/ValidateLimitations.h"
Olli Etuaho19d1dc92016-03-08 17:18:46 +020020#include "compiler/translator/ValidateMaxParameters.h"
Geoff Lang17732822013-08-29 13:46:49 -040021#include "compiler/translator/ValidateOutputs.h"
Jiawei Shao4cc89e22017-08-31 14:25:54 +080022#include "compiler/translator/ValidateVaryingLocations.h"
Geoff Lang17732822013-08-29 13:46:49 -040023#include "compiler/translator/VariablePacker.h"
Olli Etuahoa07b4212018-03-22 16:13:13 +020024#include "compiler/translator/tree_ops/AddAndTrueToLoopCondition.h"
25#include "compiler/translator/tree_ops/ClampFragDepth.h"
26#include "compiler/translator/tree_ops/ClampPointSize.h"
27#include "compiler/translator/tree_ops/DeclareAndInitBuiltinsForInstancedMultiview.h"
28#include "compiler/translator/tree_ops/DeferGlobalInitializers.h"
Austin Eng7cf9cd22018-10-09 15:27:32 -070029#include "compiler/translator/tree_ops/EmulateGLDrawID.h"
Olli Etuahoa07b4212018-03-22 16:13:13 +020030#include "compiler/translator/tree_ops/EmulateGLFragColorBroadcast.h"
31#include "compiler/translator/tree_ops/EmulatePrecision.h"
32#include "compiler/translator/tree_ops/FoldExpressions.h"
33#include "compiler/translator/tree_ops/InitializeVariables.h"
34#include "compiler/translator/tree_ops/PruneEmptyCases.h"
35#include "compiler/translator/tree_ops/PruneNoOps.h"
36#include "compiler/translator/tree_ops/RegenerateStructNames.h"
37#include "compiler/translator/tree_ops/RemoveArrayLengthMethod.h"
38#include "compiler/translator/tree_ops/RemoveInvariantDeclaration.h"
39#include "compiler/translator/tree_ops/RemovePow.h"
40#include "compiler/translator/tree_ops/RemoveUnreferencedVariables.h"
41#include "compiler/translator/tree_ops/RewriteDoWhile.h"
Olli Etuaho983460e2018-05-02 17:57:39 +030042#include "compiler/translator/tree_ops/RewriteRepeatedAssignToSwizzled.h"
Olli Etuahoa07b4212018-03-22 16:13:13 +020043#include "compiler/translator/tree_ops/ScalarizeVecAndMatConstructorArgs.h"
44#include "compiler/translator/tree_ops/SeparateDeclarations.h"
45#include "compiler/translator/tree_ops/SimplifyLoopConditions.h"
46#include "compiler/translator/tree_ops/SplitSequenceOperator.h"
47#include "compiler/translator/tree_ops/UnfoldShortCircuitAST.h"
48#include "compiler/translator/tree_ops/UseInterfaceBlockFields.h"
49#include "compiler/translator/tree_ops/VectorizeVectorScalarArithmetic.h"
Olli Etuaho59c5b892018-04-03 11:44:50 +030050#include "compiler/translator/tree_util/BuiltIn_autogen.h"
Olli Etuahoc26214d2018-03-16 10:43:11 +020051#include "compiler/translator/tree_util/IntermNodePatternMatcher.h"
Olli Etuahob5601eb2017-11-15 18:08:04 +020052#include "compiler/translator/util.h"
shannon.woods@transgaming.comda1ed362013-01-25 21:54:57 +000053#include "third_party/compiler/ArrayBoundsClamper.h"
Corentin Wallez28b65282016-06-16 07:24:50 -070054
Jamie Madillacb4b812016-11-07 13:50:29 -050055namespace sh
56{
57
Corentin Wallez28b65282016-06-16 07:24:50 -070058namespace
59{
60
61#if defined(ANGLE_ENABLE_FUZZER_CORPUS_OUTPUT)
62void DumpFuzzerCase(char const *const *shaderStrings,
63 size_t numStrings,
64 uint32_t type,
65 uint32_t spec,
66 uint32_t output,
67 uint64_t options)
68{
69 static int fileIndex = 0;
70
71 std::ostringstream o;
72 o << "corpus/" << fileIndex++ << ".sample";
73 std::string s = o.str();
74
75 // Must match the input format of the fuzzer
76 FILE *f = fopen(s.c_str(), "w");
77 fwrite(&type, sizeof(type), 1, f);
78 fwrite(&spec, sizeof(spec), 1, f);
79 fwrite(&output, sizeof(output), 1, f);
80 fwrite(&options, sizeof(options), 1, f);
81
82 char zero[128 - 20] = {0};
83 fwrite(&zero, 128 - 20, 1, f);
84
85 for (size_t i = 0; i < numStrings; i++)
86 {
87 fwrite(shaderStrings[i], sizeof(char), strlen(shaderStrings[i]), f);
88 }
89 fwrite(&zero, 1, 1, f);
90
91 fclose(f);
92}
93#endif // defined(ANGLE_ENABLE_FUZZER_CORPUS_OUTPUT)
94} // anonymous namespace
95
Qingqing Dengad0d0792015-04-08 14:25:06 -070096bool IsGLSL130OrNewer(ShShaderOutput output)
97{
Jamie Madillacb4b812016-11-07 13:50:29 -050098 return (output == SH_GLSL_130_OUTPUT || output == SH_GLSL_140_OUTPUT ||
99 output == SH_GLSL_150_CORE_OUTPUT || output == SH_GLSL_330_CORE_OUTPUT ||
100 output == SH_GLSL_400_CORE_OUTPUT || output == SH_GLSL_410_CORE_OUTPUT ||
101 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);
Qingqing Dengad0d0792015-04-08 14:25:06 -0700103}
104
Qiankun Miao705a9192016-08-29 10:05:27 +0800105bool IsGLSL420OrNewer(ShShaderOutput output)
106{
Jamie Madillacb4b812016-11-07 13:50:29 -0500107 return (output == SH_GLSL_420_CORE_OUTPUT || output == SH_GLSL_430_CORE_OUTPUT ||
108 output == SH_GLSL_440_CORE_OUTPUT || output == SH_GLSL_450_CORE_OUTPUT);
Qiankun Miao705a9192016-08-29 10:05:27 +0800109}
110
Zhenyao Mob7bf7422016-11-08 14:44:05 -0800111bool IsGLSL410OrOlder(ShShaderOutput output)
112{
113 return (output == SH_GLSL_130_OUTPUT || output == SH_GLSL_140_OUTPUT ||
114 output == SH_GLSL_150_CORE_OUTPUT || output == SH_GLSL_330_CORE_OUTPUT ||
115 output == SH_GLSL_400_CORE_OUTPUT || output == SH_GLSL_410_CORE_OUTPUT);
116}
117
Qiankun Miao89dd8f32016-11-09 12:59:30 +0000118bool RemoveInvariant(sh::GLenum shaderType,
119 int shaderVersion,
120 ShShaderOutput outputType,
121 ShCompileOptions compileOptions)
122{
123 if ((compileOptions & SH_DONT_REMOVE_INVARIANT_FOR_FRAGMENT_INPUT) == 0 &&
124 shaderType == GL_FRAGMENT_SHADER && IsGLSL420OrNewer(outputType))
125 return true;
126
127 if ((compileOptions & SH_REMOVE_INVARIANT_AND_CENTROID_FOR_ESSL3) != 0 &&
Qiankun Miao41f9f672016-11-16 17:04:36 +0800128 shaderVersion >= 300 && shaderType == GL_VERTEX_SHADER)
Qiankun Miao89dd8f32016-11-09 12:59:30 +0000129 return true;
130
131 return false;
132}
133
Zhenyao Mo7faf1a12014-04-25 18:03:56 -0700134size_t GetGlobalMaxTokenSize(ShShaderSpec spec)
Jamie Madill88f6e942014-02-19 10:27:53 -0500135{
He Yunchao29ab9ff2015-08-06 16:58:30 +0800136 // WebGL defines a max token length of 256, while ES2 leaves max token
Jamie Madill88f6e942014-02-19 10:27:53 -0500137 // size undefined. ES3 defines a max size of 1024 characters.
Zhenyao Modb9b40b2014-10-29 15:00:04 -0700138 switch (spec)
Jamie Madill88f6e942014-02-19 10:27:53 -0500139 {
Jamie Madillacb4b812016-11-07 13:50:29 -0500140 case SH_WEBGL_SPEC:
141 return 256;
142 default:
143 return 1024;
Jamie Madill88f6e942014-02-19 10:27:53 -0500144 }
145}
146
Shao77891c02017-06-23 16:30:17 +0800147int GetMaxUniformVectorsForShaderType(GLenum shaderType, const ShBuiltInResources &resources)
148{
149 switch (shaderType)
150 {
151 case GL_VERTEX_SHADER:
152 return resources.MaxVertexUniformVectors;
153 case GL_FRAGMENT_SHADER:
154 return resources.MaxFragmentUniformVectors;
Shaob5cc1192017-07-06 10:47:20 +0800155
156 // TODO (jiawei.shao@intel.com): check if we need finer-grained component counting
Shao77891c02017-06-23 16:30:17 +0800157 case GL_COMPUTE_SHADER:
Shao77891c02017-06-23 16:30:17 +0800158 return resources.MaxComputeUniformComponents / 4;
Jiawei Shaobd924af2017-11-16 15:28:04 +0800159 case GL_GEOMETRY_SHADER_EXT:
Shaob5cc1192017-07-06 10:47:20 +0800160 return resources.MaxGeometryUniformComponents / 4;
Shao77891c02017-06-23 16:30:17 +0800161 default:
Shaob5cc1192017-07-06 10:47:20 +0800162 UNREACHABLE();
Shao77891c02017-06-23 16:30:17 +0800163 return -1;
164 }
165}
166
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500167namespace
168{
Zhenyao Modb9b40b2014-10-29 15:00:04 -0700169
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800170class TScopedPoolAllocator
171{
172 public:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500173 TScopedPoolAllocator(TPoolAllocator *allocator) : mAllocator(allocator)
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800174 {
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400175 mAllocator->push();
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000176 SetGlobalPoolAllocator(mAllocator);
177 }
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800178 ~TScopedPoolAllocator()
179 {
Yunchao Hef81ce4a2017-04-24 10:49:17 +0800180 SetGlobalPoolAllocator(nullptr);
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400181 mAllocator->pop();
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000182 }
183
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800184 private:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500185 TPoolAllocator *mAllocator;
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400186};
187
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800188class TScopedSymbolTableLevel
189{
190 public:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500191 TScopedSymbolTableLevel(TSymbolTable *table) : mTable(table)
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800192 {
Olli Etuaho437664b2018-02-28 15:38:14 +0200193 ASSERT(mTable->isEmpty());
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400194 mTable->push();
195 }
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800196 ~TScopedSymbolTableLevel()
197 {
Olli Etuaho437664b2018-02-28 15:38:14 +0200198 while (!mTable->isEmpty())
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400199 mTable->pop();
200 }
201
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800202 private:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500203 TSymbolTable *mTable;
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000204};
Zhenyao Modb9b40b2014-10-29 15:00:04 -0700205
206int MapSpecToShaderVersion(ShShaderSpec spec)
207{
208 switch (spec)
209 {
Jamie Madillacb4b812016-11-07 13:50:29 -0500210 case SH_GLES2_SPEC:
211 case SH_WEBGL_SPEC:
212 return 100;
213 case SH_GLES3_SPEC:
214 case SH_WEBGL2_SPEC:
215 return 300;
216 case SH_GLES3_1_SPEC:
217 case SH_WEBGL3_SPEC:
218 return 310;
219 default:
220 UNREACHABLE();
221 return 0;
Zhenyao Modb9b40b2014-10-29 15:00:04 -0700222 }
223}
224
Olli Etuaho59c5b892018-04-03 11:44:50 +0300225bool ValidateFragColorAndFragData(GLenum shaderType,
226 int shaderVersion,
227 const TSymbolTable &symbolTable,
228 TDiagnostics *diagnostics)
229{
230 if (shaderVersion > 100 || shaderType != GL_FRAGMENT_SHADER)
231 {
232 return true;
233 }
234
235 bool usesFragColor = false;
236 bool usesFragData = false;
237 // This validation is a bit stricter than the spec - it's only an error to write to
238 // both FragData and FragColor. But because it's better not to have reads from undefined
239 // variables, we always return an error if they are both referenced, rather than only if they
240 // are written.
241 if (symbolTable.isStaticallyUsed(*BuiltInVariable::gl_FragColor()) ||
242 symbolTable.isStaticallyUsed(*BuiltInVariable::gl_SecondaryFragColorEXT()))
243 {
244 usesFragColor = true;
245 }
246 // Extension variables may not always be initialized (saves some time at symbol table init).
247 bool secondaryFragDataUsed =
248 symbolTable.gl_SecondaryFragDataEXT() != nullptr &&
249 symbolTable.isStaticallyUsed(*symbolTable.gl_SecondaryFragDataEXT());
250 if (symbolTable.isStaticallyUsed(*symbolTable.gl_FragData()) || secondaryFragDataUsed)
251 {
252 usesFragData = true;
253 }
254 if (usesFragColor && usesFragData)
255 {
256 const char *errorMessage = "cannot use both gl_FragData and gl_FragColor";
257 if (symbolTable.isStaticallyUsed(*BuiltInVariable::gl_SecondaryFragColorEXT()) ||
258 secondaryFragDataUsed)
259 {
260 errorMessage =
261 "cannot use both output variable sets (gl_FragData, gl_SecondaryFragDataEXT)"
262 " and (gl_FragColor, gl_SecondaryFragColorEXT)";
263 }
264 diagnostics->globalError(errorMessage);
265 return false;
266 }
267 return true;
268}
269
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000270} // namespace
271
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800272TShHandleBase::TShHandleBase()
273{
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000274 allocator.push();
275 SetGlobalPoolAllocator(&allocator);
276}
277
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800278TShHandleBase::~TShHandleBase()
279{
Yunchao Hef81ce4a2017-04-24 10:49:17 +0800280 SetGlobalPoolAllocator(nullptr);
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000281 allocator.popAll();
282}
283
Jamie Madill183bde52014-07-02 15:31:19 -0400284TCompiler::TCompiler(sh::GLenum type, ShShaderSpec spec, ShShaderOutput output)
Olli Etuaho6f591c92018-09-21 11:20:47 +0300285 : mVariablesCollected(false),
Olli Etuahob12040c2017-06-27 14:20:45 +0300286 mGLPositionInitialized(false),
Olli Etuaho6f591c92018-09-21 11:20:47 +0300287 mShaderType(type),
288 mShaderSpec(spec),
289 mOutputType(output),
290 mBuiltInFunctionEmulator(),
291 mDiagnostics(mInfoSink.info),
Yunchao Hef81ce4a2017-04-24 10:49:17 +0800292 mSourcePath(nullptr),
Shaob5cc1192017-07-06 10:47:20 +0800293 mComputeShaderLocalSizeDeclared(false),
Jamie Madill2f294c92017-11-20 14:47:26 -0500294 mComputeShaderLocalSize(1),
Shaob5cc1192017-07-06 10:47:20 +0800295 mGeometryShaderMaxVertices(-1),
296 mGeometryShaderInvocations(0),
297 mGeometryShaderInputPrimitiveType(EptUndefined),
298 mGeometryShaderOutputPrimitiveType(EptUndefined)
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000299{
300}
301
302TCompiler::~TCompiler()
303{
304}
305
Qiankun Miao7ebb97f2016-09-08 18:01:50 +0800306bool TCompiler::shouldRunLoopAndIndexingValidation(ShCompileOptions compileOptions) const
Olli Etuaho5d91dda2015-06-18 15:47:46 +0300307{
308 // If compiling an ESSL 1.00 shader for WebGL, or if its been requested through the API,
309 // validate loop and indexing as well (to verify that the shader only uses minimal functionality
310 // of ESSL 1.00 as in Appendix A of the spec).
Olli Etuaho6f591c92018-09-21 11:20:47 +0300311 return (IsWebGLBasedSpec(mShaderSpec) && mShaderVersion == 100) ||
Olli Etuaho5d91dda2015-06-18 15:47:46 +0300312 (compileOptions & SH_VALIDATE_LOOP_INDEXING);
313}
314
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500315bool TCompiler::Init(const ShBuiltInResources &resources)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000316{
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400317 SetGlobalPoolAllocator(&allocator);
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000318
alokp@chromium.org07620a52010-09-23 17:53:56 +0000319 // Generate built-in symbol table.
Olli Etuaho6f591c92018-09-21 11:20:47 +0300320 if (!initBuiltInSymbolTable(resources))
alokp@chromium.org07620a52010-09-23 17:53:56 +0000321 return false;
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000322
Olli Etuaho6f591c92018-09-21 11:20:47 +0300323 mResources = resources;
324 setResourceString();
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000325
Olli Etuaho6f591c92018-09-21 11:20:47 +0300326 InitExtensionBehavior(resources, mExtensionBehavior);
327 mArrayBoundsClamper.SetClampingStrategy(resources.ArrayIndexClampingStrategy);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000328 return true;
329}
330
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100331TIntermBlock *TCompiler::compileTreeForTesting(const char *const shaderStrings[],
332 size_t numStrings,
333 ShCompileOptions compileOptions)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000334{
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200335 return compileTreeImpl(shaderStrings, numStrings, compileOptions);
336}
337
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100338TIntermBlock *TCompiler::compileTreeImpl(const char *const shaderStrings[],
339 size_t numStrings,
340 const ShCompileOptions compileOptions)
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200341{
alokp@chromium.org07620a52010-09-23 17:53:56 +0000342 clearResults();
343
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200344 ASSERT(numStrings > 0);
345 ASSERT(GetGlobalPoolAllocator());
alokp@chromium.org07620a52010-09-23 17:53:56 +0000346
David Yen0fbd1282015-02-02 14:46:09 -0800347 // Reset the extension behavior for each compilation unit.
Olli Etuaho6f591c92018-09-21 11:20:47 +0300348 ResetExtensionBehavior(mExtensionBehavior);
David Yen0fbd1282015-02-02 14:46:09 -0800349
Austin Eng7cf9cd22018-10-09 15:27:32 -0700350 // If gl_DrawID is not supported, remove it from the available extensions
351 // Currently we only allow emulation of gl_DrawID
352 const bool glDrawIDSupported = (compileOptions & SH_EMULATE_GL_DRAW_ID) != 0u;
353 if (!glDrawIDSupported)
354 {
355 auto it = mExtensionBehavior.find(TExtension::ANGLE_multi_draw);
356 if (it != mExtensionBehavior.end())
357 {
358 mExtensionBehavior.erase(it);
359 }
360 }
361
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000362 // First string is path of source file if flag is set. The actual source follows.
shannon.woods@transgaming.comd64b3da2013-02-28 23:19:26 +0000363 size_t firstSource = 0;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000364 if (compileOptions & SH_SOURCE_PATH)
365 {
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200366 mSourcePath = shaderStrings[0];
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000367 ++firstSource;
368 }
369
Olli Etuaho6f591c92018-09-21 11:20:47 +0300370 TParseContext parseContext(mSymbolTable, mExtensionBehavior, mShaderType, mShaderSpec,
Olli Etuaho77ba4082016-12-16 12:01:18 +0000371 compileOptions, true, &mDiagnostics, getResources());
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200372
Olli Etuaho6f591c92018-09-21 11:20:47 +0300373 parseContext.setFragmentPrecisionHighOnESSL1(mResources.FragmentPrecisionHigh == 1);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000374
375 // We preserve symbols at the built-in level from compile-to-compile.
376 // Start pushing the user-defined symbols at global level.
Olli Etuaho6f591c92018-09-21 11:20:47 +0300377 TScopedSymbolTableLevel globalLevel(&mSymbolTable);
378 ASSERT(mSymbolTable.atGlobalLevel());
alokp@chromium.org07620a52010-09-23 17:53:56 +0000379
380 // Parse shader.
Olli Etuahod10cf692017-11-02 11:06:14 +0200381 if (PaParseStrings(numStrings - firstSource, &shaderStrings[firstSource], nullptr,
382 &parseContext) != 0)
383 {
384 return nullptr;
385 }
shannon.woods%transgaming.com@gtempaccount.com0bbed382013-04-13 03:38:07 +0000386
Olli Etuahod10cf692017-11-02 11:06:14 +0200387 if (parseContext.getTreeRoot() == nullptr)
388 {
389 return nullptr;
390 }
391
392 setASTMetadata(parseContext);
393
Jiawei Shaoe41df652018-02-09 14:31:39 +0800394 if (!checkShaderVersion(&parseContext))
Zhenyao Modb9b40b2014-10-29 15:00:04 -0700395 {
Olli Etuahod10cf692017-11-02 11:06:14 +0200396 return nullptr;
Zhenyao Modb9b40b2014-10-29 15:00:04 -0700397 }
shannon.woods%transgaming.com@gtempaccount.com0bbed382013-04-13 03:38:07 +0000398
Olli Etuahod10cf692017-11-02 11:06:14 +0200399 TIntermBlock *root = parseContext.getTreeRoot();
400 if (!checkAndSimplifyAST(root, parseContext, compileOptions))
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800401 {
Olli Etuahod10cf692017-11-02 11:06:14 +0200402 return nullptr;
403 }
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700404
Olli Etuahod10cf692017-11-02 11:06:14 +0200405 return root;
406}
Martin Radev802abe02016-08-04 17:48:32 +0300407
Jiawei Shaoe41df652018-02-09 14:31:39 +0800408bool TCompiler::checkShaderVersion(TParseContext *parseContext)
409{
Olli Etuaho6f591c92018-09-21 11:20:47 +0300410 if (MapSpecToShaderVersion(mShaderSpec) < mShaderVersion)
Jiawei Shaoe41df652018-02-09 14:31:39 +0800411 {
412 mDiagnostics.globalError("unsupported shader version");
413 return false;
414 }
415
416 ASSERT(parseContext);
Olli Etuaho6f591c92018-09-21 11:20:47 +0300417 switch (mShaderType)
Jiawei Shaoe41df652018-02-09 14:31:39 +0800418 {
419 case GL_COMPUTE_SHADER:
Olli Etuaho6f591c92018-09-21 11:20:47 +0300420 if (mShaderVersion < 310)
Jiawei Shaoe41df652018-02-09 14:31:39 +0800421 {
422 mDiagnostics.globalError("Compute shader is not supported in this shader version.");
423 return false;
424 }
425 break;
426
427 case GL_GEOMETRY_SHADER_EXT:
Olli Etuaho6f591c92018-09-21 11:20:47 +0300428 if (mShaderVersion < 310)
Jiawei Shaoe41df652018-02-09 14:31:39 +0800429 {
430 mDiagnostics.globalError(
431 "Geometry shader is not supported in this shader version.");
432 return false;
433 }
434 else
435 {
Olli Etuaho6f591c92018-09-21 11:20:47 +0300436 ASSERT(mShaderVersion == 310);
Jiawei Shaoe41df652018-02-09 14:31:39 +0800437 if (!parseContext->checkCanUseExtension(sh::TSourceLoc(),
438 TExtension::EXT_geometry_shader))
439 {
440 return false;
441 }
442 }
443 break;
444
445 default:
446 break;
447 }
448
449 return true;
450}
451
Olli Etuahod10cf692017-11-02 11:06:14 +0200452void TCompiler::setASTMetadata(const TParseContext &parseContext)
453{
Olli Etuaho6f591c92018-09-21 11:20:47 +0300454 mShaderVersion = parseContext.getShaderVersion();
Olli Etuaho09b04a22016-12-15 13:30:26 +0000455
Olli Etuahod10cf692017-11-02 11:06:14 +0200456 mPragma = parseContext.pragma();
Olli Etuaho6f591c92018-09-21 11:20:47 +0300457 mSymbolTable.setGlobalInvariant(mPragma.stdgl.invariantAll);
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000458
Olli Etuahod10cf692017-11-02 11:06:14 +0200459 mComputeShaderLocalSizeDeclared = parseContext.isComputeShaderLocalSizeDeclared();
460 mComputeShaderLocalSize = parseContext.getComputeShaderLocalSize();
Olli Etuahoa6996682015-10-12 14:32:30 +0300461
Olli Etuahod10cf692017-11-02 11:06:14 +0200462 mNumViews = parseContext.getNumViews();
463
Olli Etuaho6f591c92018-09-21 11:20:47 +0300464 if (mShaderType == GL_GEOMETRY_SHADER_EXT)
Olli Etuahod10cf692017-11-02 11:06:14 +0200465 {
466 mGeometryShaderInputPrimitiveType = parseContext.getGeometryShaderInputPrimitiveType();
467 mGeometryShaderOutputPrimitiveType = parseContext.getGeometryShaderOutputPrimitiveType();
468 mGeometryShaderMaxVertices = parseContext.getGeometryShaderMaxVertices();
469 mGeometryShaderInvocations = parseContext.getGeometryShaderInvocations();
470 }
471}
472
473bool TCompiler::checkAndSimplifyAST(TIntermBlock *root,
474 const TParseContext &parseContext,
475 ShCompileOptions compileOptions)
476{
477 // Disallow expressions deemed too complex.
478 if ((compileOptions & SH_LIMIT_EXPRESSION_COMPLEXITY) && !limitExpressionComplexity(root))
479 {
480 return false;
481 }
482
Olli Etuaho765924f2018-01-04 12:48:36 +0200483 if (shouldRunLoopAndIndexingValidation(compileOptions) &&
Olli Etuaho6f591c92018-09-21 11:20:47 +0300484 !ValidateLimitations(root, mShaderType, &mSymbolTable, &mDiagnostics))
Olli Etuaho765924f2018-01-04 12:48:36 +0200485 {
486 return false;
487 }
488
Olli Etuaho6f591c92018-09-21 11:20:47 +0300489 if (!ValidateFragColorAndFragData(mShaderType, mShaderVersion, mSymbolTable, &mDiagnostics))
Olli Etuaho59c5b892018-04-03 11:44:50 +0300490 {
491 return false;
492 }
493
Olli Etuaho765924f2018-01-04 12:48:36 +0200494 // Fold expressions that could not be folded before validation that was done as a part of
495 // parsing.
496 FoldExpressions(root, &mDiagnostics);
497 // Folding should only be able to generate warnings.
498 ASSERT(mDiagnostics.numErrors() == 0);
499
Olli Etuahod10cf692017-11-02 11:06:14 +0200500 // We prune no-ops to work around driver bugs and to keep AST processing and output simple.
501 // The following kinds of no-ops are pruned:
502 // 1. Empty declarations "int;".
503 // 2. Literal statements: "1.0;". The ESSL output doesn't define a default precision
504 // for float, so float literal statements would end up with no precision which is
505 // invalid ESSL.
506 // After this empty declarations are not allowed in the AST.
Olli Etuaho6f591c92018-09-21 11:20:47 +0300507 PruneNoOps(root, &mSymbolTable);
Olli Etuahod10cf692017-11-02 11:06:14 +0200508
Olli Etuahod10cf692017-11-02 11:06:14 +0200509 // Create the function DAG and check there is no recursion
510 if (!initCallDag(root))
511 {
512 return false;
513 }
514
515 if ((compileOptions & SH_LIMIT_CALL_STACK_DEPTH) && !checkCallDepth())
516 {
517 return false;
518 }
519
520 // Checks which functions are used and if "main" exists
Olli Etuaho6f591c92018-09-21 11:20:47 +0300521 mFunctionMetadata.clear();
522 mFunctionMetadata.resize(mCallDag.size());
Olli Etuahod10cf692017-11-02 11:06:14 +0200523 if (!tagUsedFunctions())
524 {
525 return false;
526 }
527
528 if (!(compileOptions & SH_DONT_PRUNE_UNUSED_FUNCTIONS))
529 {
530 pruneUnusedFunctions(root);
531 }
532
Olli Etuaho6f591c92018-09-21 11:20:47 +0300533 if (mShaderVersion >= 310 && !ValidateVaryingLocations(root, &mDiagnostics, mShaderType))
Olli Etuahod10cf692017-11-02 11:06:14 +0200534 {
535 return false;
536 }
537
Olli Etuaho6f591c92018-09-21 11:20:47 +0300538 if (mShaderVersion >= 300 && mShaderType == GL_FRAGMENT_SHADER &&
539 !ValidateOutputs(root, getExtensionBehavior(), mResources.MaxDrawBuffers, &mDiagnostics))
Olli Etuahod10cf692017-11-02 11:06:14 +0200540 {
541 return false;
542 }
543
Olli Etuahod10cf692017-11-02 11:06:14 +0200544 // Fail compilation if precision emulation not supported.
545 if (getResources().WEBGL_debug_shader_precision && getPragma().debugShaderPrecision &&
Olli Etuaho6f591c92018-09-21 11:20:47 +0300546 !EmulatePrecision::SupportedInLanguage(mOutputType))
Olli Etuahod10cf692017-11-02 11:06:14 +0200547 {
548 mDiagnostics.globalError("Precision emulation not supported for this output type.");
549 return false;
550 }
551
Olli Etuahod10cf692017-11-02 11:06:14 +0200552 // Clamping uniform array bounds needs to happen after validateLimitations pass.
553 if (compileOptions & SH_CLAMP_INDIRECT_ARRAY_BOUNDS)
554 {
Olli Etuaho6f591c92018-09-21 11:20:47 +0300555 mArrayBoundsClamper.MarkIndirectArrayBoundsForClamping(root);
Olli Etuahod10cf692017-11-02 11:06:14 +0200556 }
557
558 if ((compileOptions & SH_INITIALIZE_BUILTINS_FOR_INSTANCED_MULTIVIEW) &&
559 parseContext.isExtensionEnabled(TExtension::OVR_multiview) &&
560 getShaderType() != GL_COMPUTE_SHADER)
561 {
Olli Etuaho6f591c92018-09-21 11:20:47 +0300562 DeclareAndInitBuiltinsForInstancedMultiview(root, mNumViews, mShaderType, compileOptions,
563 mOutputType, &mSymbolTable);
Olli Etuahod10cf692017-11-02 11:06:14 +0200564 }
565
566 // This pass might emit short circuits so keep it before the short circuit unfolding
567 if (compileOptions & SH_REWRITE_DO_WHILE_LOOPS)
Olli Etuaho6f591c92018-09-21 11:20:47 +0300568 RewriteDoWhile(root, &mSymbolTable);
Olli Etuahod10cf692017-11-02 11:06:14 +0200569
570 if (compileOptions & SH_ADD_AND_TRUE_TO_LOOP_CONDITION)
Olli Etuaho1776fd02018-01-31 11:46:52 +0200571 AddAndTrueToLoopCondition(root);
Olli Etuahod10cf692017-11-02 11:06:14 +0200572
573 if (compileOptions & SH_UNFOLD_SHORT_CIRCUIT)
574 {
Olli Etuaho1776fd02018-01-31 11:46:52 +0200575 UnfoldShortCircuitAST(root);
Olli Etuahod10cf692017-11-02 11:06:14 +0200576 }
577
578 if (compileOptions & SH_REMOVE_POW_WITH_CONSTANT_EXPONENT)
579 {
Olli Etuaho6f591c92018-09-21 11:20:47 +0300580 RemovePow(root, &mSymbolTable);
Olli Etuahod10cf692017-11-02 11:06:14 +0200581 }
582
Olli Etuahod10cf692017-11-02 11:06:14 +0200583 if (compileOptions & SH_REGENERATE_STRUCT_NAMES)
584 {
Olli Etuaho6f591c92018-09-21 11:20:47 +0300585 RegenerateStructNames gen(&mSymbolTable);
Olli Etuahod10cf692017-11-02 11:06:14 +0200586 root->traverse(&gen);
587 }
588
Austin Eng7cf9cd22018-10-09 15:27:32 -0700589 if (mShaderType == GL_VERTEX_SHADER &&
590 IsExtensionEnabled(mExtensionBehavior, TExtension::ANGLE_multi_draw))
591 {
592 if ((compileOptions & SH_EMULATE_GL_DRAW_ID) != 0)
593 {
594 EmulateGLDrawID(root, &mSymbolTable, &mUniforms,
595 shouldCollectVariables(compileOptions));
596 }
597 }
598
Olli Etuaho6f591c92018-09-21 11:20:47 +0300599 if (mShaderType == GL_FRAGMENT_SHADER && mShaderVersion == 100 && mResources.EXT_draw_buffers &&
600 mResources.MaxDrawBuffers > 1 &&
601 IsExtensionEnabled(mExtensionBehavior, TExtension::EXT_draw_buffers))
Olli Etuahod10cf692017-11-02 11:06:14 +0200602 {
Olli Etuaho6f591c92018-09-21 11:20:47 +0300603 EmulateGLFragColorBroadcast(root, mResources.MaxDrawBuffers, &mOutputVariables,
604 &mSymbolTable, mShaderVersion);
Olli Etuahod10cf692017-11-02 11:06:14 +0200605 }
606
Olli Etuahoae04e1e2017-11-27 16:00:39 +0200607 int simplifyScalarized = (compileOptions & SH_SCALARIZE_VEC_AND_MAT_CONSTRUCTOR_ARGS)
608 ? IntermNodePatternMatcher::kScalarizedVecOrMatConstructor
609 : 0;
610
Olli Etuahod10cf692017-11-02 11:06:14 +0200611 // Split multi declarations and remove calls to array length().
612 // Note that SimplifyLoopConditions needs to be run before any other AST transformations
613 // that may need to generate new statements from loop conditions or loop expressions.
Olli Etuahoae04e1e2017-11-27 16:00:39 +0200614 SimplifyLoopConditions(root,
615 IntermNodePatternMatcher::kMultiDeclaration |
616 IntermNodePatternMatcher::kArrayLengthMethod | simplifyScalarized,
Olli Etuaho68981eb2018-01-23 17:46:12 +0200617 &getSymbolTable());
Olli Etuahod10cf692017-11-02 11:06:14 +0200618
619 // Note that separate declarations need to be run before other AST transformations that
620 // generate new statements from expressions.
621 SeparateDeclarations(root);
622
Olli Etuahoae04e1e2017-11-27 16:00:39 +0200623 SplitSequenceOperator(root, IntermNodePatternMatcher::kArrayLengthMethod | simplifyScalarized,
Olli Etuaho68981eb2018-01-23 17:46:12 +0200624 &getSymbolTable());
Olli Etuahod10cf692017-11-02 11:06:14 +0200625
626 RemoveArrayLengthMethod(root);
627
Olli Etuaho6f591c92018-09-21 11:20:47 +0300628 RemoveUnreferencedVariables(root, &mSymbolTable);
Olli Etuaho3d70ca92017-11-10 16:53:26 +0200629
Olli Etuaho41217992018-03-15 11:15:33 +0200630 // In case the last case inside a switch statement is a certain type of no-op, GLSL compilers in
631 // drivers may not accept it. In this case we clean up the dead code from the end of switch
632 // statements. This is also required because PruneNoOps or RemoveUnreferencedVariables may have
633 // left switch statements that only contained an empty declaration inside the final case in an
634 // invalid state. Relies on that PruneNoOps and RemoveUnreferencedVariables have already been
635 // run.
Olli Etuaho0b0dcbc2018-03-14 11:33:22 +0200636 PruneEmptyCases(root);
637
Olli Etuaho3d70ca92017-11-10 16:53:26 +0200638 // Built-in function emulation needs to happen after validateLimitations pass.
639 // TODO(jmadill): Remove global pool allocator.
640 GetGlobalPoolAllocator()->lock();
Olli Etuaho6f591c92018-09-21 11:20:47 +0300641 initBuiltInFunctionEmulator(&mBuiltInFunctionEmulator, compileOptions);
Olli Etuaho3d70ca92017-11-10 16:53:26 +0200642 GetGlobalPoolAllocator()->unlock();
Olli Etuaho6f591c92018-09-21 11:20:47 +0300643 mBuiltInFunctionEmulator.markBuiltInFunctionsForEmulation(root);
Olli Etuaho3d70ca92017-11-10 16:53:26 +0200644
Olli Etuaho6f591c92018-09-21 11:20:47 +0300645 bool highPrecisionSupported = mShaderVersion > 100 || mShaderType != GL_FRAGMENT_SHADER ||
646 mResources.FragmentPrecisionHigh == 1;
Olli Etuaho3d70ca92017-11-10 16:53:26 +0200647 if (compileOptions & SH_SCALARIZE_VEC_AND_MAT_CONSTRUCTOR_ARGS)
648 {
Olli Etuaho6f591c92018-09-21 11:20:47 +0300649 ScalarizeVecAndMatConstructorArgs(root, mShaderType, highPrecisionSupported, &mSymbolTable);
Olli Etuaho3d70ca92017-11-10 16:53:26 +0200650 }
651
652 if (shouldCollectVariables(compileOptions))
653 {
Olli Etuaho6f591c92018-09-21 11:20:47 +0300654 ASSERT(!mVariablesCollected);
655 CollectVariables(root, &mAttributes, &mOutputVariables, &mUniforms, &mInputVaryings,
656 &mOutputVaryings, &mUniformBlocks, &mShaderStorageBlocks, &mInBlocks,
657 mResources.HashFunction, &mSymbolTable, mShaderType, mExtensionBehavior);
Olli Etuaho3d70ca92017-11-10 16:53:26 +0200658 collectInterfaceBlocks();
Olli Etuaho6f591c92018-09-21 11:20:47 +0300659 mVariablesCollected = true;
Olli Etuaho3d70ca92017-11-10 16:53:26 +0200660 if (compileOptions & SH_USE_UNUSED_STANDARD_SHARED_BLOCKS)
661 {
662 useAllMembersInUnusedStandardAndSharedBlocks(root);
663 }
664 if (compileOptions & SH_ENFORCE_PACKING_RESTRICTIONS)
665 {
Olli Etuaho6f591c92018-09-21 11:20:47 +0300666 int maxUniformVectors = GetMaxUniformVectorsForShaderType(mShaderType, mResources);
Olli Etuaho3d70ca92017-11-10 16:53:26 +0200667 // Returns true if, after applying the packing rules in the GLSL ES 1.00.17 spec
668 // Appendix A, section 7, the shader does not use too many uniforms.
Olli Etuaho6f591c92018-09-21 11:20:47 +0300669 if (!CheckVariablesInPackingLimits(maxUniformVectors, mUniforms))
Olli Etuaho3d70ca92017-11-10 16:53:26 +0200670 {
671 mDiagnostics.globalError("too many uniforms");
672 return false;
673 }
674 }
Olli Etuaho6f591c92018-09-21 11:20:47 +0300675 if ((compileOptions & SH_INIT_OUTPUT_VARIABLES) && (mShaderType != GL_COMPUTE_SHADER))
Olli Etuaho3d70ca92017-11-10 16:53:26 +0200676 {
677 initializeOutputVariables(root);
678 }
679 }
680
681 // Removing invariant declarations must be done after collecting variables.
682 // Otherwise, built-in invariant declarations don't apply.
Olli Etuaho6f591c92018-09-21 11:20:47 +0300683 if (RemoveInvariant(mShaderType, mShaderVersion, mOutputType, compileOptions))
Olli Etuaho3d70ca92017-11-10 16:53:26 +0200684 {
685 RemoveInvariantDeclaration(root);
686 }
687
688 // gl_Position is always written in compatibility output mode.
689 // It may have been already initialized among other output variables, in that case we don't
690 // need to initialize it twice.
Olli Etuaho6f591c92018-09-21 11:20:47 +0300691 if (mShaderType == GL_VERTEX_SHADER && !mGLPositionInitialized &&
692 ((compileOptions & SH_INIT_GL_POSITION) || (mOutputType == SH_GLSL_COMPATIBILITY_OUTPUT)))
Olli Etuaho3d70ca92017-11-10 16:53:26 +0200693 {
694 initializeGLPosition(root);
695 mGLPositionInitialized = true;
696 }
697
Olli Etuahob5601eb2017-11-15 18:08:04 +0200698 // DeferGlobalInitializers needs to be run before other AST transformations that generate new
699 // statements from expressions. But it's fine to run DeferGlobalInitializers after the above
700 // SplitSequenceOperator and RemoveArrayLengthMethod since they only have an effect on the AST
701 // on ESSL >= 3.00, and the initializers that need to be deferred can only exist in ESSL < 3.00.
702 bool initializeLocalsAndGlobals =
703 (compileOptions & SH_INITIALIZE_UNINITIALIZED_LOCALS) && !IsOutputHLSL(getOutputType());
Olli Etuaho2c7f34c2017-10-09 17:18:02 +0300704 bool canUseLoopsToInitialize = !(compileOptions & SH_DONT_USE_LOOPS_TO_INITIALIZE_VARIABLES);
Olli Etuaho87cc90d2017-12-12 15:28:06 +0200705 DeferGlobalInitializers(root, initializeLocalsAndGlobals, canUseLoopsToInitialize,
Olli Etuaho6f591c92018-09-21 11:20:47 +0300706 highPrecisionSupported, &mSymbolTable);
Olli Etuaho2c7f34c2017-10-09 17:18:02 +0300707
Olli Etuahob5601eb2017-11-15 18:08:04 +0200708 if (initializeLocalsAndGlobals)
Olli Etuahod10cf692017-11-02 11:06:14 +0200709 {
710 // Initialize uninitialized local variables.
711 // In some cases initializing can generate extra statements in the parent block, such as
712 // when initializing nameless structs or initializing arrays in ESSL 1.00. In that case
713 // we need to first simplify loop conditions. We've already separated declarations
714 // earlier, which is also required. If we don't follow the Appendix A limitations, loop
715 // init statements can declare arrays or nameless structs and have multiple
716 // declarations.
717
718 if (!shouldRunLoopAndIndexingValidation(compileOptions))
719 {
720 SimplifyLoopConditions(root,
721 IntermNodePatternMatcher::kArrayDeclaration |
722 IntermNodePatternMatcher::kNamelessStructDeclaration,
Olli Etuaho68981eb2018-01-23 17:46:12 +0200723 &getSymbolTable());
Olli Etuahod10cf692017-11-02 11:06:14 +0200724 }
Olli Etuaho661fc482017-10-16 12:17:05 +0300725
Olli Etuaho2c7f34c2017-10-09 17:18:02 +0300726 InitializeUninitializedLocals(root, getShaderVersion(), canUseLoopsToInitialize,
Olli Etuaho87cc90d2017-12-12 15:28:06 +0200727 highPrecisionSupported, &getSymbolTable());
Olli Etuahod10cf692017-11-02 11:06:14 +0200728 }
729
730 if (getShaderType() == GL_VERTEX_SHADER && (compileOptions & SH_CLAMP_POINT_SIZE))
731 {
Olli Etuaho6f591c92018-09-21 11:20:47 +0300732 ClampPointSize(root, mResources.MaxPointSize, &getSymbolTable());
Olli Etuahod10cf692017-11-02 11:06:14 +0200733 }
734
Olli Etuaho0690e1a2017-12-21 20:51:38 +0200735 if (getShaderType() == GL_FRAGMENT_SHADER && (compileOptions & SH_CLAMP_FRAG_DEPTH))
736 {
737 ClampFragDepth(root, &getSymbolTable());
738 }
739
Olli Etuaho983460e2018-05-02 17:57:39 +0300740 if (compileOptions & SH_REWRITE_REPEATED_ASSIGN_TO_SWIZZLED)
741 {
742 sh::RewriteRepeatedAssignToSwizzled(root);
743 }
744
Olli Etuaho661fc482017-10-16 12:17:05 +0300745 if (compileOptions & SH_REWRITE_VECTOR_SCALAR_ARITHMETIC)
746 {
747 VectorizeVectorScalarArithmetic(root, &getSymbolTable());
748 }
749
Olli Etuahod10cf692017-11-02 11:06:14 +0200750 return true;
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200751}
752
Qiankun Miao7ebb97f2016-09-08 18:01:50 +0800753bool TCompiler::compile(const char *const shaderStrings[],
754 size_t numStrings,
755 ShCompileOptions compileOptionsIn)
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200756{
Corentin Wallez28b65282016-06-16 07:24:50 -0700757#if defined(ANGLE_ENABLE_FUZZER_CORPUS_OUTPUT)
Olli Etuaho6f591c92018-09-21 11:20:47 +0300758 DumpFuzzerCase(shaderStrings, numStrings, mShaderType, mShaderSpec, mOutputType,
759 compileOptionsIn);
Corentin Wallez28b65282016-06-16 07:24:50 -0700760#endif // defined(ANGLE_ENABLE_FUZZER_CORPUS_OUTPUT)
761
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200762 if (numStrings == 0)
763 return true;
764
Qiankun Miao7ebb97f2016-09-08 18:01:50 +0800765 ShCompileOptions compileOptions = compileOptionsIn;
Kenneth Russellbccc65d2016-07-19 16:48:43 -0700766
767 // Apply key workarounds.
768 if (shouldFlattenPragmaStdglInvariantAll())
769 {
770 // This should be harmless to do in all cases, but for the moment, do it only conditionally.
771 compileOptions |= SH_FLATTEN_PRAGMA_STDGL_INVARIANT_ALL;
772 }
773
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200774 TScopedPoolAllocator scopedAlloc(&allocator);
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100775 TIntermBlock *root = compileTreeImpl(shaderStrings, numStrings, compileOptions);
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200776
777 if (root)
778 {
779 if (compileOptions & SH_INTERMEDIATE_TREE)
Olli Etuaho6f591c92018-09-21 11:20:47 +0300780 OutputTree(root, mInfoSink.info);
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200781
782 if (compileOptions & SH_OBJECT_CODE)
Olli Etuaho89a69a02017-10-23 12:20:45 +0300783 {
784 PerformanceDiagnostics perfDiagnostics(&mDiagnostics);
785 translate(root, compileOptions, &perfDiagnostics);
786 }
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200787
Austin Eng7cf9cd22018-10-09 15:27:32 -0700788 if (mShaderType == GL_VERTEX_SHADER &&
789 IsExtensionEnabled(mExtensionBehavior, TExtension::ANGLE_multi_draw))
790 {
791 if ((compileOptions & SH_EMULATE_GL_DRAW_ID) != 0)
792 {
793 for (auto& uniform : mUniforms)
794 {
795 if (uniform.name == "angle_DrawID" && uniform.mappedName == "angle_DrawID")
796 {
797 uniform.name = "gl_DrawID";
798 break;
799 }
800 }
801 }
802 }
803
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200804 // The IntermNode tree doesn't need to be deleted here, since the
805 // memory will be freed in a big chunk by the PoolAllocator.
806 return true;
807 }
808 return false;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000809}
810
Olli Etuaho6f591c92018-09-21 11:20:47 +0300811bool TCompiler::initBuiltInSymbolTable(const ShBuiltInResources &resources)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000812{
Olli Etuaho28cb0362016-11-22 15:42:37 +0000813 if (resources.MaxDrawBuffers < 1)
814 {
815 return false;
816 }
817 if (resources.EXT_blend_func_extended && resources.MaxDualSourceDrawBuffers < 1)
818 {
819 return false;
820 }
821
Olli Etuaho6f591c92018-09-21 11:20:47 +0300822 mSymbolTable.initializeBuiltIns(mShaderType, mShaderSpec, resources);
Olli Etuaho5d69db12017-11-24 16:51:15 +0200823
Nicolas Capens49a88872013-06-20 09:54:03 -0400824 return true;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000825}
826
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400827void TCompiler::setResourceString()
828{
829 std::ostringstream strstream;
Geoff Langb66a9092016-05-16 15:59:14 -0400830
831 // clang-format off
Olli Etuaho6f591c92018-09-21 11:20:47 +0300832 strstream << ":MaxVertexAttribs:" << mResources.MaxVertexAttribs
833 << ":MaxVertexUniformVectors:" << mResources.MaxVertexUniformVectors
834 << ":MaxVaryingVectors:" << mResources.MaxVaryingVectors
835 << ":MaxVertexTextureImageUnits:" << mResources.MaxVertexTextureImageUnits
836 << ":MaxCombinedTextureImageUnits:" << mResources.MaxCombinedTextureImageUnits
837 << ":MaxTextureImageUnits:" << mResources.MaxTextureImageUnits
838 << ":MaxFragmentUniformVectors:" << mResources.MaxFragmentUniformVectors
839 << ":MaxDrawBuffers:" << mResources.MaxDrawBuffers
840 << ":OES_standard_derivatives:" << mResources.OES_standard_derivatives
841 << ":OES_EGL_image_external:" << mResources.OES_EGL_image_external
842 << ":OES_EGL_image_external_essl3:" << mResources.OES_EGL_image_external_essl3
843 << ":NV_EGL_stream_consumer_external:" << mResources.NV_EGL_stream_consumer_external
844 << ":ARB_texture_rectangle:" << mResources.ARB_texture_rectangle
845 << ":EXT_draw_buffers:" << mResources.EXT_draw_buffers
846 << ":FragmentPrecisionHigh:" << mResources.FragmentPrecisionHigh
847 << ":MaxExpressionComplexity:" << mResources.MaxExpressionComplexity
848 << ":MaxCallStackDepth:" << mResources.MaxCallStackDepth
849 << ":MaxFunctionParameters:" << mResources.MaxFunctionParameters
850 << ":EXT_blend_func_extended:" << mResources.EXT_blend_func_extended
851 << ":EXT_frag_depth:" << mResources.EXT_frag_depth
852 << ":EXT_shader_texture_lod:" << mResources.EXT_shader_texture_lod
853 << ":EXT_shader_framebuffer_fetch:" << mResources.EXT_shader_framebuffer_fetch
854 << ":NV_shader_framebuffer_fetch:" << mResources.NV_shader_framebuffer_fetch
855 << ":ARM_shader_framebuffer_fetch:" << mResources.ARM_shader_framebuffer_fetch
856 << ":OVR_multiview:" << mResources.OVR_multiview
857 << ":EXT_YUV_target:" << mResources.EXT_YUV_target
858 << ":EXT_geometry_shader:" << mResources.EXT_geometry_shader
859 << ":MaxVertexOutputVectors:" << mResources.MaxVertexOutputVectors
860 << ":MaxFragmentInputVectors:" << mResources.MaxFragmentInputVectors
861 << ":MinProgramTexelOffset:" << mResources.MinProgramTexelOffset
862 << ":MaxProgramTexelOffset:" << mResources.MaxProgramTexelOffset
863 << ":MaxDualSourceDrawBuffers:" << mResources.MaxDualSourceDrawBuffers
864 << ":MaxViewsOVR:" << mResources.MaxViewsOVR
865 << ":NV_draw_buffers:" << mResources.NV_draw_buffers
866 << ":WEBGL_debug_shader_precision:" << mResources.WEBGL_debug_shader_precision
Austin Eng7cf9cd22018-10-09 15:27:32 -0700867 << ":ANGLE_multi_draw:" << mResources.ANGLE_multi_draw
Olli Etuaho6f591c92018-09-21 11:20:47 +0300868 << ":MinProgramTextureGatherOffset:" << mResources.MinProgramTextureGatherOffset
869 << ":MaxProgramTextureGatherOffset:" << mResources.MaxProgramTextureGatherOffset
870 << ":MaxImageUnits:" << mResources.MaxImageUnits
871 << ":MaxVertexImageUniforms:" << mResources.MaxVertexImageUniforms
872 << ":MaxFragmentImageUniforms:" << mResources.MaxFragmentImageUniforms
873 << ":MaxComputeImageUniforms:" << mResources.MaxComputeImageUniforms
874 << ":MaxCombinedImageUniforms:" << mResources.MaxCombinedImageUniforms
875 << ":MaxCombinedShaderOutputResources:" << mResources.MaxCombinedShaderOutputResources
876 << ":MaxComputeWorkGroupCountX:" << mResources.MaxComputeWorkGroupCount[0]
877 << ":MaxComputeWorkGroupCountY:" << mResources.MaxComputeWorkGroupCount[1]
878 << ":MaxComputeWorkGroupCountZ:" << mResources.MaxComputeWorkGroupCount[2]
879 << ":MaxComputeWorkGroupSizeX:" << mResources.MaxComputeWorkGroupSize[0]
880 << ":MaxComputeWorkGroupSizeY:" << mResources.MaxComputeWorkGroupSize[1]
881 << ":MaxComputeWorkGroupSizeZ:" << mResources.MaxComputeWorkGroupSize[2]
882 << ":MaxComputeUniformComponents:" << mResources.MaxComputeUniformComponents
883 << ":MaxComputeTextureImageUnits:" << mResources.MaxComputeTextureImageUnits
884 << ":MaxComputeAtomicCounters:" << mResources.MaxComputeAtomicCounters
885 << ":MaxComputeAtomicCounterBuffers:" << mResources.MaxComputeAtomicCounterBuffers
886 << ":MaxVertexAtomicCounters:" << mResources.MaxVertexAtomicCounters
887 << ":MaxFragmentAtomicCounters:" << mResources.MaxFragmentAtomicCounters
888 << ":MaxCombinedAtomicCounters:" << mResources.MaxCombinedAtomicCounters
889 << ":MaxAtomicCounterBindings:" << mResources.MaxAtomicCounterBindings
890 << ":MaxVertexAtomicCounterBuffers:" << mResources.MaxVertexAtomicCounterBuffers
891 << ":MaxFragmentAtomicCounterBuffers:" << mResources.MaxFragmentAtomicCounterBuffers
892 << ":MaxCombinedAtomicCounterBuffers:" << mResources.MaxCombinedAtomicCounterBuffers
893 << ":MaxAtomicCounterBufferSize:" << mResources.MaxAtomicCounterBufferSize
894 << ":MaxGeometryUniformComponents:" << mResources.MaxGeometryUniformComponents
895 << ":MaxGeometryUniformBlocks:" << mResources.MaxGeometryUniformBlocks
896 << ":MaxGeometryInputComponents:" << mResources.MaxGeometryInputComponents
897 << ":MaxGeometryOutputComponents:" << mResources.MaxGeometryOutputComponents
898 << ":MaxGeometryOutputVertices:" << mResources.MaxGeometryOutputVertices
899 << ":MaxGeometryTotalOutputComponents:" << mResources.MaxGeometryTotalOutputComponents
900 << ":MaxGeometryTextureImageUnits:" << mResources.MaxGeometryTextureImageUnits
901 << ":MaxGeometryAtomicCounterBuffers:" << mResources.MaxGeometryAtomicCounterBuffers
902 << ":MaxGeometryAtomicCounters:" << mResources.MaxGeometryAtomicCounters
903 << ":MaxGeometryShaderStorageBlocks:" << mResources.MaxGeometryShaderStorageBlocks
904 << ":MaxGeometryShaderInvocations:" << mResources.MaxGeometryShaderInvocations
905 << ":MaxGeometryImageUniforms:" << mResources.MaxGeometryImageUniforms;
Geoff Langb66a9092016-05-16 15:59:14 -0400906 // clang-format on
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400907
Olli Etuaho6f591c92018-09-21 11:20:47 +0300908 mBuiltInResourcesString = strstream.str();
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400909}
910
Jiajia Qin9b11ea42017-07-11 16:50:08 +0800911void TCompiler::collectInterfaceBlocks()
912{
Olli Etuaho6f591c92018-09-21 11:20:47 +0300913 ASSERT(mInterfaceBlocks.empty());
914 mInterfaceBlocks.reserve(mUniformBlocks.size() + mShaderStorageBlocks.size() +
915 mInBlocks.size());
916 mInterfaceBlocks.insert(mInterfaceBlocks.end(), mUniformBlocks.begin(), mUniformBlocks.end());
917 mInterfaceBlocks.insert(mInterfaceBlocks.end(), mShaderStorageBlocks.begin(),
918 mShaderStorageBlocks.end());
919 mInterfaceBlocks.insert(mInterfaceBlocks.end(), mInBlocks.begin(), mInBlocks.end());
Jiajia Qin9b11ea42017-07-11 16:50:08 +0800920}
921
alokp@chromium.org07620a52010-09-23 17:53:56 +0000922void TCompiler::clearResults()
923{
Olli Etuaho6f591c92018-09-21 11:20:47 +0300924 mArrayBoundsClamper.Cleanup();
925 mInfoSink.info.erase();
926 mInfoSink.obj.erase();
927 mInfoSink.debug.erase();
Olli Etuaho77ba4082016-12-16 12:01:18 +0000928 mDiagnostics.resetErrorCount();
alokp@chromium.org07620a52010-09-23 17:53:56 +0000929
Olli Etuaho6f591c92018-09-21 11:20:47 +0300930 mAttributes.clear();
931 mOutputVariables.clear();
932 mUniforms.clear();
933 mInputVaryings.clear();
934 mOutputVaryings.clear();
935 mInterfaceBlocks.clear();
936 mUniformBlocks.clear();
937 mShaderStorageBlocks.clear();
938 mInBlocks.clear();
939 mVariablesCollected = false;
Olli Etuahob12040c2017-06-27 14:20:45 +0300940 mGLPositionInitialized = false;
zmo@google.coma3b4ab42011-09-16 00:53:26 +0000941
Olli Etuaho09b04a22016-12-15 13:30:26 +0000942 mNumViews = -1;
943
Shaob5cc1192017-07-06 10:47:20 +0800944 mGeometryShaderInputPrimitiveType = EptUndefined;
945 mGeometryShaderOutputPrimitiveType = EptUndefined;
946 mGeometryShaderInvocations = 0;
947 mGeometryShaderMaxVertices = -1;
948
Olli Etuaho6f591c92018-09-21 11:20:47 +0300949 mBuiltInFunctionEmulator.cleanup();
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +0000950
Olli Etuaho6f591c92018-09-21 11:20:47 +0300951 mNameMap.clear();
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200952
Yunchao Hed7297bf2017-04-19 15:27:10 +0800953 mSourcePath = nullptr;
Olli Etuaho5d69db12017-11-24 16:51:15 +0200954
Olli Etuaho6f591c92018-09-21 11:20:47 +0300955 mSymbolTable.clearCompilationResults();
alokp@chromium.org07620a52010-09-23 17:53:56 +0000956}
957
Corentin Wallez71d147f2015-02-11 11:15:24 -0800958bool TCompiler::initCallDag(TIntermNode *root)
zmo@google.comb1762df2011-07-30 02:04:23 +0000959{
Corentin Wallez71d147f2015-02-11 11:15:24 -0800960 mCallDag.clear();
961
Olli Etuaho77ba4082016-12-16 12:01:18 +0000962 switch (mCallDag.init(root, &mDiagnostics))
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800963 {
Jamie Madillacb4b812016-11-07 13:50:29 -0500964 case CallDAG::INITDAG_SUCCESS:
965 return true;
966 case CallDAG::INITDAG_RECURSION:
Jamie Madillacb4b812016-11-07 13:50:29 -0500967 case CallDAG::INITDAG_UNDEFINED:
Olli Etuaho77ba4082016-12-16 12:01:18 +0000968 // Error message has already been written out.
969 ASSERT(mDiagnostics.numErrors() > 0);
Jamie Madillacb4b812016-11-07 13:50:29 -0500970 return false;
Corentin Wallez71d147f2015-02-11 11:15:24 -0800971 }
972
973 UNREACHABLE();
974 return true;
975}
976
977bool TCompiler::checkCallDepth()
978{
979 std::vector<int> depths(mCallDag.size());
980
981 for (size_t i = 0; i < mCallDag.size(); i++)
982 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500983 int depth = 0;
Corentin Wallez71d147f2015-02-11 11:15:24 -0800984 auto &record = mCallDag.getRecordFromIndex(i);
985
986 for (auto &calleeIndex : record.callees)
987 {
988 depth = std::max(depth, depths[calleeIndex] + 1);
989 }
990
991 depths[i] = depth;
992
Olli Etuaho6f591c92018-09-21 11:20:47 +0300993 if (depth >= mResources.MaxCallStackDepth)
Corentin Wallez71d147f2015-02-11 11:15:24 -0800994 {
995 // Trace back the function chain to have a meaningful info log.
Olli Etuaho77ba4082016-12-16 12:01:18 +0000996 std::stringstream errorStream;
Olli Etuaho6f591c92018-09-21 11:20:47 +0300997 errorStream << "Call stack too deep (larger than " << mResources.MaxCallStackDepth
Olli Etuahoc33f1e82017-12-29 16:55:29 +0200998 << ") with the following call chain: "
999 << record.node->getFunction()->name();
Corentin Wallez71d147f2015-02-11 11:15:24 -08001000
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001001 int currentFunction = static_cast<int>(i);
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001002 int currentDepth = depth;
Corentin Wallez71d147f2015-02-11 11:15:24 -08001003
1004 while (currentFunction != -1)
1005 {
Olli Etuahoc33f1e82017-12-29 16:55:29 +02001006 errorStream
1007 << " -> "
1008 << mCallDag.getRecordFromIndex(currentFunction).node->getFunction()->name();
Corentin Wallez71d147f2015-02-11 11:15:24 -08001009
1010 int nextFunction = -1;
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001011 for (auto &calleeIndex : mCallDag.getRecordFromIndex(currentFunction).callees)
Corentin Wallez71d147f2015-02-11 11:15:24 -08001012 {
1013 if (depths[calleeIndex] == currentDepth - 1)
1014 {
1015 currentDepth--;
1016 nextFunction = calleeIndex;
1017 }
1018 }
1019
1020 currentFunction = nextFunction;
1021 }
1022
Olli Etuaho77ba4082016-12-16 12:01:18 +00001023 std::string errorStr = errorStream.str();
1024 mDiagnostics.globalError(errorStr.c_str());
1025
Corentin Wallez71d147f2015-02-11 11:15:24 -08001026 return false;
1027 }
1028 }
1029
1030 return true;
1031}
1032
1033bool TCompiler::tagUsedFunctions()
1034{
1035 // Search from main, starting from the end of the DAG as it usually is the root.
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001036 for (size_t i = mCallDag.size(); i-- > 0;)
Corentin Wallez71d147f2015-02-11 11:15:24 -08001037 {
Olli Etuahoc33f1e82017-12-29 16:55:29 +02001038 if (mCallDag.getRecordFromIndex(i).node->getFunction()->isMain())
Corentin Wallez71d147f2015-02-11 11:15:24 -08001039 {
1040 internalTagUsedFunction(i);
1041 return true;
1042 }
1043 }
1044
Olli Etuaho77ba4082016-12-16 12:01:18 +00001045 mDiagnostics.globalError("Missing main()");
Corentin Wallez71d147f2015-02-11 11:15:24 -08001046 return false;
1047}
1048
1049void TCompiler::internalTagUsedFunction(size_t index)
1050{
Olli Etuaho6f591c92018-09-21 11:20:47 +03001051 if (mFunctionMetadata[index].used)
Corentin Wallez71d147f2015-02-11 11:15:24 -08001052 {
1053 return;
1054 }
1055
Olli Etuaho6f591c92018-09-21 11:20:47 +03001056 mFunctionMetadata[index].used = true;
Corentin Wallez71d147f2015-02-11 11:15:24 -08001057
1058 for (int calleeIndex : mCallDag.getRecordFromIndex(index).callees)
1059 {
1060 internalTagUsedFunction(calleeIndex);
zmo@google.comb1762df2011-07-30 02:04:23 +00001061 }
1062}
1063
Corentin Walleza094a8a2015-04-07 11:53:06 -07001064// A predicate for the stl that returns if a top-level node is unused
1065class TCompiler::UnusedPredicate
1066{
1067 public:
1068 UnusedPredicate(const CallDAG *callDag, const std::vector<FunctionMetadata> *metadatas)
Jamie Madillacb4b812016-11-07 13:50:29 -05001069 : mCallDag(callDag), mMetadatas(metadatas)
Corentin Walleza094a8a2015-04-07 11:53:06 -07001070 {
1071 }
1072
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001073 bool operator()(TIntermNode *node)
Corentin Walleza094a8a2015-04-07 11:53:06 -07001074 {
Olli Etuaho16c745a2017-01-16 17:02:27 +00001075 const TIntermFunctionPrototype *asFunctionPrototype = node->getAsFunctionPrototypeNode();
1076 const TIntermFunctionDefinition *asFunctionDefinition = node->getAsFunctionDefinition();
Corentin Walleza094a8a2015-04-07 11:53:06 -07001077
Olli Etuahobeb6dc72017-12-14 16:03:03 +02001078 const TFunction *func = nullptr;
Olli Etuaho336b1472016-10-05 16:37:55 +01001079
Olli Etuaho16c745a2017-01-16 17:02:27 +00001080 if (asFunctionDefinition)
Olli Etuaho336b1472016-10-05 16:37:55 +01001081 {
Olli Etuahobeb6dc72017-12-14 16:03:03 +02001082 func = asFunctionDefinition->getFunction();
Olli Etuaho336b1472016-10-05 16:37:55 +01001083 }
Olli Etuaho16c745a2017-01-16 17:02:27 +00001084 else if (asFunctionPrototype)
Olli Etuaho336b1472016-10-05 16:37:55 +01001085 {
Olli Etuahobeb6dc72017-12-14 16:03:03 +02001086 func = asFunctionPrototype->getFunction();
Olli Etuaho336b1472016-10-05 16:37:55 +01001087 }
Olli Etuahobeb6dc72017-12-14 16:03:03 +02001088 if (func == nullptr)
Corentin Walleza094a8a2015-04-07 11:53:06 -07001089 {
1090 return false;
1091 }
1092
Olli Etuahobeb6dc72017-12-14 16:03:03 +02001093 size_t callDagIndex = mCallDag->findIndex(func->uniqueId());
Corentin Walleza094a8a2015-04-07 11:53:06 -07001094 if (callDagIndex == CallDAG::InvalidIndex)
1095 {
1096 // This happens only for unimplemented prototypes which are thus unused
Olli Etuaho16c745a2017-01-16 17:02:27 +00001097 ASSERT(asFunctionPrototype);
Corentin Walleza094a8a2015-04-07 11:53:06 -07001098 return true;
1099 }
1100
1101 ASSERT(callDagIndex < mMetadatas->size());
1102 return !(*mMetadatas)[callDagIndex].used;
1103 }
1104
1105 private:
1106 const CallDAG *mCallDag;
1107 const std::vector<FunctionMetadata> *mMetadatas;
1108};
1109
Olli Etuahod10cf692017-11-02 11:06:14 +02001110void TCompiler::pruneUnusedFunctions(TIntermBlock *root)
Corentin Walleza094a8a2015-04-07 11:53:06 -07001111{
Olli Etuaho6f591c92018-09-21 11:20:47 +03001112 UnusedPredicate isUnused(&mCallDag, &mFunctionMetadata);
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01001113 TIntermSequence *sequence = root->getSequence();
Corentin Wallezb081e782015-07-20 05:40:04 -07001114
1115 if (!sequence->empty())
1116 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001117 sequence->erase(std::remove_if(sequence->begin(), sequence->end(), isUnused),
1118 sequence->end());
Corentin Wallezb081e782015-07-20 05:40:04 -07001119 }
Corentin Walleza094a8a2015-04-07 11:53:06 -07001120}
1121
Olli Etuahob4279202017-05-18 15:08:24 +03001122bool TCompiler::limitExpressionComplexity(TIntermBlock *root)
Jamie Madilleb1a0102013-07-08 13:31:38 -04001123{
Olli Etuaho6f591c92018-09-21 11:20:47 +03001124 if (!IsASTDepthBelowLimit(root, mResources.MaxExpressionComplexity))
Jamie Madill6654bc92014-03-26 14:01:57 -04001125 {
Olli Etuaho77ba4082016-12-16 12:01:18 +00001126 mDiagnostics.globalError("Expression too complex.");
Jamie Madill6654bc92014-03-26 14:01:57 -04001127 return false;
1128 }
1129
Olli Etuaho6f591c92018-09-21 11:20:47 +03001130 if (!ValidateMaxParameters(root, mResources.MaxFunctionParameters))
Olli Etuaho19d1dc92016-03-08 17:18:46 +02001131 {
Olli Etuaho77ba4082016-12-16 12:01:18 +00001132 mDiagnostics.globalError("Function has too many parameters.");
Olli Etuaho19d1dc92016-03-08 17:18:46 +02001133 return false;
1134 }
1135
Jamie Madilleb1a0102013-07-08 13:31:38 -04001136 return true;
1137}
1138
Corentin Wallez1df16022016-10-27 08:16:56 -04001139bool TCompiler::shouldCollectVariables(ShCompileOptions compileOptions)
1140{
1141 return (compileOptions & SH_VARIABLES) != 0;
1142}
1143
1144bool TCompiler::wereVariablesCollected() const
1145{
Olli Etuaho6f591c92018-09-21 11:20:47 +03001146 return mVariablesCollected;
Corentin Wallez1df16022016-10-27 08:16:56 -04001147}
1148
Olli Etuaho9cbc07c2017-05-10 18:22:01 +03001149void TCompiler::initializeGLPosition(TIntermBlock *root)
Zhenyao Mo4a667fe2014-02-11 12:35:01 -08001150{
Zhenyao Mo72111912016-07-20 17:45:56 -07001151 InitVariableList list;
Olli Etuahoe7c28572017-10-23 16:29:33 +03001152 sh::ShaderVariable var(GL_FLOAT_VEC4);
Zhenyao Mo72111912016-07-20 17:45:56 -07001153 var.name = "gl_Position";
1154 list.push_back(var);
Olli Etuaho6f591c92018-09-21 11:20:47 +03001155 InitializeVariables(root, list, &mSymbolTable, mShaderVersion, mExtensionBehavior, false,
1156 false);
Zhenyao Mo4a667fe2014-02-11 12:35:01 -08001157}
1158
Olli Etuaho9cbc07c2017-05-10 18:22:01 +03001159void TCompiler::useAllMembersInUnusedStandardAndSharedBlocks(TIntermBlock *root)
Qin Jiajia7835b522016-10-08 11:20:17 +08001160{
1161 sh::InterfaceBlockList list;
1162
Olli Etuaho6f591c92018-09-21 11:20:47 +03001163 for (auto block : mUniformBlocks)
Qin Jiajia7835b522016-10-08 11:20:17 +08001164 {
1165 if (!block.staticUse &&
Qin Jiajiaca68d982017-09-18 16:41:56 +08001166 (block.layout == sh::BLOCKLAYOUT_STD140 || block.layout == sh::BLOCKLAYOUT_SHARED))
Qin Jiajia7835b522016-10-08 11:20:17 +08001167 {
1168 list.push_back(block);
1169 }
1170 }
1171
Olli Etuaho6f591c92018-09-21 11:20:47 +03001172 sh::UseInterfaceBlockFields(root, list, mSymbolTable);
Qin Jiajia7835b522016-10-08 11:20:17 +08001173}
1174
Olli Etuaho9cbc07c2017-05-10 18:22:01 +03001175void TCompiler::initializeOutputVariables(TIntermBlock *root)
Zhenyao Mo4a667fe2014-02-11 12:35:01 -08001176{
Zhenyao Mo72111912016-07-20 17:45:56 -07001177 InitVariableList list;
Olli Etuaho6f591c92018-09-21 11:20:47 +03001178 if (mShaderType == GL_VERTEX_SHADER || mShaderType == GL_GEOMETRY_SHADER_EXT)
Zhenyao Mo4a667fe2014-02-11 12:35:01 -08001179 {
Olli Etuaho6f591c92018-09-21 11:20:47 +03001180 for (auto var : mOutputVaryings)
Zhenyao Mo4a667fe2014-02-11 12:35:01 -08001181 {
Zhenyao Mof9312682016-07-22 12:51:31 -07001182 list.push_back(var);
Olli Etuahob12040c2017-06-27 14:20:45 +03001183 if (var.name == "gl_Position")
1184 {
1185 ASSERT(!mGLPositionInitialized);
1186 mGLPositionInitialized = true;
1187 }
Zhenyao Mo4a667fe2014-02-11 12:35:01 -08001188 }
Zhenyao Mo4a667fe2014-02-11 12:35:01 -08001189 }
Zhenyao Mo72111912016-07-20 17:45:56 -07001190 else
1191 {
Olli Etuaho6f591c92018-09-21 11:20:47 +03001192 ASSERT(mShaderType == GL_FRAGMENT_SHADER);
1193 for (auto var : mOutputVariables)
Zhenyao Mo72111912016-07-20 17:45:56 -07001194 {
1195 list.push_back(var);
1196 }
1197 }
Olli Etuaho6f591c92018-09-21 11:20:47 +03001198 InitializeVariables(root, list, &mSymbolTable, mShaderVersion, mExtensionBehavior, false,
1199 false);
Zhenyao Mo4a667fe2014-02-11 12:35:01 -08001200}
1201
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001202const TExtensionBehavior &TCompiler::getExtensionBehavior() const
zmo@google.com5601ea02011-06-10 18:23:25 +00001203{
Olli Etuaho6f591c92018-09-21 11:20:47 +03001204 return mExtensionBehavior;
zmo@google.com5601ea02011-06-10 18:23:25 +00001205}
zmo@google.com32e97312011-08-24 01:03:11 +00001206
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02001207const char *TCompiler::getSourcePath() const
1208{
1209 return mSourcePath;
1210}
1211
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001212const ShBuiltInResources &TCompiler::getResources() const
shannon.woods%transgaming.com@gtempaccount.com18b4c4b2013-04-13 03:31:40 +00001213{
Olli Etuaho6f591c92018-09-21 11:20:47 +03001214 return mResources;
shannon.woods%transgaming.com@gtempaccount.com18b4c4b2013-04-13 03:31:40 +00001215}
1216
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001217const ArrayBoundsClamper &TCompiler::getArrayBoundsClamper() const
daniel@transgaming.com4167cc92013-01-11 04:11:53 +00001218{
Olli Etuaho6f591c92018-09-21 11:20:47 +03001219 return mArrayBoundsClamper;
daniel@transgaming.com4167cc92013-01-11 04:11:53 +00001220}
1221
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +00001222ShArrayIndexClampingStrategy TCompiler::getArrayIndexClampingStrategy() const
1223{
Olli Etuaho6f591c92018-09-21 11:20:47 +03001224 return mResources.ArrayIndexClampingStrategy;
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +00001225}
1226
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001227const BuiltInFunctionEmulator &TCompiler::getBuiltInFunctionEmulator() const
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +00001228{
Olli Etuaho6f591c92018-09-21 11:20:47 +03001229 return mBuiltInFunctionEmulator;
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +00001230}
Zhenyao Mo94ac7b72014-10-15 18:22:08 -07001231
Qiankun Miao7ebb97f2016-09-08 18:01:50 +08001232void TCompiler::writePragma(ShCompileOptions compileOptions)
Zhenyao Mo94ac7b72014-10-15 18:22:08 -07001233{
Kenneth Russellbccc65d2016-07-19 16:48:43 -07001234 if (!(compileOptions & SH_FLATTEN_PRAGMA_STDGL_INVARIANT_ALL))
1235 {
Olli Etuaho6f591c92018-09-21 11:20:47 +03001236 TInfoSinkBase &sink = mInfoSink.obj;
Kenneth Russellbccc65d2016-07-19 16:48:43 -07001237 if (mPragma.stdgl.invariantAll)
1238 sink << "#pragma STDGL invariant(all)\n";
1239 }
1240}
1241
1242bool TCompiler::isVaryingDefined(const char *varyingName)
1243{
Olli Etuaho6f591c92018-09-21 11:20:47 +03001244 ASSERT(mVariablesCollected);
1245 for (size_t ii = 0; ii < mInputVaryings.size(); ++ii)
Kenneth Russellbccc65d2016-07-19 16:48:43 -07001246 {
Olli Etuaho6f591c92018-09-21 11:20:47 +03001247 if (mInputVaryings[ii].name == varyingName)
Jiawei-Shaodf7d7c92017-07-31 09:34:04 +08001248 {
1249 return true;
1250 }
1251 }
Olli Etuaho6f591c92018-09-21 11:20:47 +03001252 for (size_t ii = 0; ii < mOutputVaryings.size(); ++ii)
Jiawei-Shaodf7d7c92017-07-31 09:34:04 +08001253 {
Olli Etuaho6f591c92018-09-21 11:20:47 +03001254 if (mOutputVaryings[ii].name == varyingName)
Kenneth Russellbccc65d2016-07-19 16:48:43 -07001255 {
1256 return true;
1257 }
1258 }
1259
1260 return false;
Zhenyao Mo94ac7b72014-10-15 18:22:08 -07001261}
Jamie Madillacb4b812016-11-07 13:50:29 -05001262
1263} // namespace sh