blob: 959c10ed0049e630fc7d42d70d41b2772bb3853b [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"
Corentin Wallez71d147f2015-02-11 11:15:24 -080014#include "compiler/translator/CallDAG.h"
Olli Etuahoab918822017-07-14 17:03:42 +030015#include "compiler/translator/ClampPointSize.h"
Olli Etuaho78ed6cd2017-08-09 16:19:00 +030016#include "compiler/translator/CollectVariables.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 Etuaho87c35882017-10-19 15:19:38 +030027#include "compiler/translator/PruneNoOps.h"
Zhenyao Moe740add2014-07-18 17:01:01 -070028#include "compiler/translator/RegenerateStructNames.h"
Olli Etuahobb5a7e22017-08-30 13:03:12 +030029#include "compiler/translator/RemoveArrayLengthMethod.h"
Olli Etuahoc1f14fb2017-10-28 19:17:23 +030030#include "compiler/translator/RemoveEmptySwitchStatements.h"
Qiankun Miao705a9192016-08-29 10:05:27 +080031#include "compiler/translator/RemoveInvariantDeclaration.h"
Olli Etuaho923ecef2017-10-11 12:01:38 +030032#include "compiler/translator/RemoveNoOpCasesFromEndOfSwitchStatements.h"
Olli Etuaho5c407bb2015-06-01 12:20:39 +030033#include "compiler/translator/RemovePow.h"
Olli Etuaho3d70ca92017-11-10 16:53:26 +020034#include "compiler/translator/RemoveUnreferencedVariables.h"
Corentin Wallezd4b50542015-09-28 12:19:26 -070035#include "compiler/translator/RewriteDoWhile.h"
Zhenyao Mocd68fe72014-07-11 10:45:44 -070036#include "compiler/translator/ScalarizeVecAndMatConstructorArgs.h"
Olli Etuaho9733cee2017-05-11 19:14:35 +030037#include "compiler/translator/SeparateDeclarations.h"
38#include "compiler/translator/SimplifyLoopConditions.h"
Olli Etuahobb5a7e22017-08-30 13:03:12 +030039#include "compiler/translator/SplitSequenceOperator.h"
Zhenyao Mo7cab38b2013-10-15 12:59:30 -070040#include "compiler/translator/UnfoldShortCircuitAST.h"
Qin Jiajia7835b522016-10-08 11:20:17 +080041#include "compiler/translator/UseInterfaceBlockFields.h"
Geoff Lang17732822013-08-29 13:46:49 -040042#include "compiler/translator/ValidateLimitations.h"
Olli Etuaho19d1dc92016-03-08 17:18:46 +020043#include "compiler/translator/ValidateMaxParameters.h"
Geoff Lang17732822013-08-29 13:46:49 -040044#include "compiler/translator/ValidateOutputs.h"
Jiawei Shao4cc89e22017-08-31 14:25:54 +080045#include "compiler/translator/ValidateVaryingLocations.h"
Geoff Lang17732822013-08-29 13:46:49 -040046#include "compiler/translator/VariablePacker.h"
Olli Etuaho661fc482017-10-16 12:17:05 +030047#include "compiler/translator/VectorizeVectorScalarArithmetic.h"
Olli Etuahob5601eb2017-11-15 18:08:04 +020048#include "compiler/translator/util.h"
shannon.woods@transgaming.comda1ed362013-01-25 21:54:57 +000049#include "third_party/compiler/ArrayBoundsClamper.h"
Corentin Wallez28b65282016-06-16 07:24:50 -070050
Jamie Madillacb4b812016-11-07 13:50:29 -050051namespace sh
52{
53
Corentin Wallez28b65282016-06-16 07:24:50 -070054namespace
55{
56
57#if defined(ANGLE_ENABLE_FUZZER_CORPUS_OUTPUT)
58void DumpFuzzerCase(char const *const *shaderStrings,
59 size_t numStrings,
60 uint32_t type,
61 uint32_t spec,
62 uint32_t output,
63 uint64_t options)
64{
65 static int fileIndex = 0;
66
67 std::ostringstream o;
68 o << "corpus/" << fileIndex++ << ".sample";
69 std::string s = o.str();
70
71 // Must match the input format of the fuzzer
72 FILE *f = fopen(s.c_str(), "w");
73 fwrite(&type, sizeof(type), 1, f);
74 fwrite(&spec, sizeof(spec), 1, f);
75 fwrite(&output, sizeof(output), 1, f);
76 fwrite(&options, sizeof(options), 1, f);
77
78 char zero[128 - 20] = {0};
79 fwrite(&zero, 128 - 20, 1, f);
80
81 for (size_t i = 0; i < numStrings; i++)
82 {
83 fwrite(shaderStrings[i], sizeof(char), strlen(shaderStrings[i]), f);
84 }
85 fwrite(&zero, 1, 1, f);
86
87 fclose(f);
88}
89#endif // defined(ANGLE_ENABLE_FUZZER_CORPUS_OUTPUT)
90} // anonymous namespace
91
Jamie Madill5508f392014-02-20 13:31:36 -050092bool IsWebGLBasedSpec(ShShaderSpec spec)
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +000093{
Qiankun Miaoc2c5fc42016-08-31 15:24:22 +080094 return (spec == SH_WEBGL_SPEC || spec == SH_WEBGL2_SPEC || spec == SH_WEBGL3_SPEC);
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +000095}
96
Qingqing Dengad0d0792015-04-08 14:25:06 -070097bool IsGLSL130OrNewer(ShShaderOutput output)
98{
Jamie Madillacb4b812016-11-07 13:50:29 -050099 return (output == SH_GLSL_130_OUTPUT || output == SH_GLSL_140_OUTPUT ||
100 output == SH_GLSL_150_CORE_OUTPUT || output == SH_GLSL_330_CORE_OUTPUT ||
101 output == SH_GLSL_400_CORE_OUTPUT || output == SH_GLSL_410_CORE_OUTPUT ||
102 output == SH_GLSL_420_CORE_OUTPUT || output == SH_GLSL_430_CORE_OUTPUT ||
103 output == SH_GLSL_440_CORE_OUTPUT || output == SH_GLSL_450_CORE_OUTPUT);
Qingqing Dengad0d0792015-04-08 14:25:06 -0700104}
105
Qiankun Miao705a9192016-08-29 10:05:27 +0800106bool IsGLSL420OrNewer(ShShaderOutput output)
107{
Jamie Madillacb4b812016-11-07 13:50:29 -0500108 return (output == SH_GLSL_420_CORE_OUTPUT || output == SH_GLSL_430_CORE_OUTPUT ||
109 output == SH_GLSL_440_CORE_OUTPUT || output == SH_GLSL_450_CORE_OUTPUT);
Qiankun Miao705a9192016-08-29 10:05:27 +0800110}
111
Zhenyao Mob7bf7422016-11-08 14:44:05 -0800112bool IsGLSL410OrOlder(ShShaderOutput output)
113{
114 return (output == SH_GLSL_130_OUTPUT || output == SH_GLSL_140_OUTPUT ||
115 output == SH_GLSL_150_CORE_OUTPUT || output == SH_GLSL_330_CORE_OUTPUT ||
116 output == SH_GLSL_400_CORE_OUTPUT || output == SH_GLSL_410_CORE_OUTPUT);
117}
118
Qiankun Miao89dd8f32016-11-09 12:59:30 +0000119bool RemoveInvariant(sh::GLenum shaderType,
120 int shaderVersion,
121 ShShaderOutput outputType,
122 ShCompileOptions compileOptions)
123{
124 if ((compileOptions & SH_DONT_REMOVE_INVARIANT_FOR_FRAGMENT_INPUT) == 0 &&
125 shaderType == GL_FRAGMENT_SHADER && IsGLSL420OrNewer(outputType))
126 return true;
127
128 if ((compileOptions & SH_REMOVE_INVARIANT_AND_CENTROID_FOR_ESSL3) != 0 &&
Qiankun Miao41f9f672016-11-16 17:04:36 +0800129 shaderVersion >= 300 && shaderType == GL_VERTEX_SHADER)
Qiankun Miao89dd8f32016-11-09 12:59:30 +0000130 return true;
131
132 return false;
133}
134
Zhenyao Mo7faf1a12014-04-25 18:03:56 -0700135size_t GetGlobalMaxTokenSize(ShShaderSpec spec)
Jamie Madill88f6e942014-02-19 10:27:53 -0500136{
He Yunchao29ab9ff2015-08-06 16:58:30 +0800137 // WebGL defines a max token length of 256, while ES2 leaves max token
Jamie Madill88f6e942014-02-19 10:27:53 -0500138 // size undefined. ES3 defines a max size of 1024 characters.
Zhenyao Modb9b40b2014-10-29 15:00:04 -0700139 switch (spec)
Jamie Madill88f6e942014-02-19 10:27:53 -0500140 {
Jamie Madillacb4b812016-11-07 13:50:29 -0500141 case SH_WEBGL_SPEC:
142 return 256;
143 default:
144 return 1024;
Jamie Madill88f6e942014-02-19 10:27:53 -0500145 }
146}
147
Shao77891c02017-06-23 16:30:17 +0800148int GetMaxUniformVectorsForShaderType(GLenum shaderType, const ShBuiltInResources &resources)
149{
150 switch (shaderType)
151 {
152 case GL_VERTEX_SHADER:
153 return resources.MaxVertexUniformVectors;
154 case GL_FRAGMENT_SHADER:
155 return resources.MaxFragmentUniformVectors;
Shaob5cc1192017-07-06 10:47:20 +0800156
157 // TODO (jiawei.shao@intel.com): check if we need finer-grained component counting
Shao77891c02017-06-23 16:30:17 +0800158 case GL_COMPUTE_SHADER:
Shao77891c02017-06-23 16:30:17 +0800159 return resources.MaxComputeUniformComponents / 4;
Shaob5cc1192017-07-06 10:47:20 +0800160 case GL_GEOMETRY_SHADER_OES:
161 return resources.MaxGeometryUniformComponents / 4;
Shao77891c02017-06-23 16:30:17 +0800162 default:
Shaob5cc1192017-07-06 10:47:20 +0800163 UNREACHABLE();
Shao77891c02017-06-23 16:30:17 +0800164 return -1;
165 }
166}
167
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500168namespace
169{
Zhenyao Modb9b40b2014-10-29 15:00:04 -0700170
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800171class TScopedPoolAllocator
172{
173 public:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500174 TScopedPoolAllocator(TPoolAllocator *allocator) : mAllocator(allocator)
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800175 {
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400176 mAllocator->push();
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000177 SetGlobalPoolAllocator(mAllocator);
178 }
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800179 ~TScopedPoolAllocator()
180 {
Yunchao Hef81ce4a2017-04-24 10:49:17 +0800181 SetGlobalPoolAllocator(nullptr);
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400182 mAllocator->pop();
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000183 }
184
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800185 private:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500186 TPoolAllocator *mAllocator;
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400187};
188
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800189class TScopedSymbolTableLevel
190{
191 public:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500192 TScopedSymbolTableLevel(TSymbolTable *table) : mTable(table)
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800193 {
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400194 ASSERT(mTable->atBuiltInLevel());
195 mTable->push();
196 }
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800197 ~TScopedSymbolTableLevel()
198 {
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400199 while (!mTable->atBuiltInLevel())
200 mTable->pop();
201 }
202
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800203 private:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500204 TSymbolTable *mTable;
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000205};
Zhenyao Modb9b40b2014-10-29 15:00:04 -0700206
207int MapSpecToShaderVersion(ShShaderSpec spec)
208{
209 switch (spec)
210 {
Jamie Madillacb4b812016-11-07 13:50:29 -0500211 case SH_GLES2_SPEC:
212 case SH_WEBGL_SPEC:
213 return 100;
214 case SH_GLES3_SPEC:
215 case SH_WEBGL2_SPEC:
216 return 300;
217 case SH_GLES3_1_SPEC:
218 case SH_WEBGL3_SPEC:
219 return 310;
220 default:
221 UNREACHABLE();
222 return 0;
Zhenyao Modb9b40b2014-10-29 15:00:04 -0700223 }
224}
225
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000226} // namespace
227
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800228TShHandleBase::TShHandleBase()
229{
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000230 allocator.push();
231 SetGlobalPoolAllocator(&allocator);
232}
233
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800234TShHandleBase::~TShHandleBase()
235{
Yunchao Hef81ce4a2017-04-24 10:49:17 +0800236 SetGlobalPoolAllocator(nullptr);
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000237 allocator.popAll();
238}
239
Jamie Madill183bde52014-07-02 15:31:19 -0400240TCompiler::TCompiler(sh::GLenum type, ShShaderSpec spec, ShShaderOutput output)
Kenneth Russellbccc65d2016-07-19 16:48:43 -0700241 : variablesCollected(false),
Olli Etuahob12040c2017-06-27 14:20:45 +0300242 mGLPositionInitialized(false),
Kenneth Russellbccc65d2016-07-19 16:48:43 -0700243 shaderType(type),
zmo@google.comf420c422011-09-12 18:27:59 +0000244 shaderSpec(spec),
Jamie Madill68fe74a2014-05-27 12:56:01 -0400245 outputType(output),
Jamie Madilleb1a0102013-07-08 13:31:38 -0400246 maxUniformVectors(0),
247 maxExpressionComplexity(0),
248 maxCallStackDepth(0),
Olli Etuaho19d1dc92016-03-08 17:18:46 +0200249 maxFunctionParameters(0),
shannon.woods%transgaming.com@gtempaccount.comcbb6b6a2013-04-13 03:27:47 +0000250 fragmentPrecisionHigh(false),
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000251 clampingStrategy(SH_CLAMP_WITH_CLAMP_INTRINSIC),
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200252 builtInFunctionEmulator(),
Olli Etuaho77ba4082016-12-16 12:01:18 +0000253 mDiagnostics(infoSink.info),
Yunchao Hef81ce4a2017-04-24 10:49:17 +0800254 mSourcePath(nullptr),
Shaob5cc1192017-07-06 10:47:20 +0800255 mComputeShaderLocalSizeDeclared(false),
Jamie Madill2f294c92017-11-20 14:47:26 -0500256 mComputeShaderLocalSize(1),
Shaob5cc1192017-07-06 10:47:20 +0800257 mGeometryShaderMaxVertices(-1),
258 mGeometryShaderInvocations(0),
259 mGeometryShaderInputPrimitiveType(EptUndefined),
260 mGeometryShaderOutputPrimitiveType(EptUndefined)
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000261{
262}
263
264TCompiler::~TCompiler()
265{
266}
267
Qiankun Miao7ebb97f2016-09-08 18:01:50 +0800268bool TCompiler::shouldRunLoopAndIndexingValidation(ShCompileOptions compileOptions) const
Olli Etuaho5d91dda2015-06-18 15:47:46 +0300269{
270 // If compiling an ESSL 1.00 shader for WebGL, or if its been requested through the API,
271 // validate loop and indexing as well (to verify that the shader only uses minimal functionality
272 // of ESSL 1.00 as in Appendix A of the spec).
273 return (IsWebGLBasedSpec(shaderSpec) && shaderVersion == 100) ||
274 (compileOptions & SH_VALIDATE_LOOP_INDEXING);
275}
276
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500277bool TCompiler::Init(const ShBuiltInResources &resources)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000278{
Shao77891c02017-06-23 16:30:17 +0800279 shaderVersion = 100;
280
281 maxUniformVectors = GetMaxUniformVectorsForShaderType(shaderType, resources);
282
Jamie Madilleb1a0102013-07-08 13:31:38 -0400283 maxExpressionComplexity = resources.MaxExpressionComplexity;
Olli Etuaho19d1dc92016-03-08 17:18:46 +0200284 maxCallStackDepth = resources.MaxCallStackDepth;
285 maxFunctionParameters = resources.MaxFunctionParameters;
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400286
287 SetGlobalPoolAllocator(&allocator);
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000288
alokp@chromium.org07620a52010-09-23 17:53:56 +0000289 // Generate built-in symbol table.
290 if (!InitBuiltInSymbolTable(resources))
291 return false;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000292 InitExtensionBehavior(resources, extensionBehavior);
shannon.woods%transgaming.com@gtempaccount.comcbb6b6a2013-04-13 03:27:47 +0000293 fragmentPrecisionHigh = resources.FragmentPrecisionHigh == 1;
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000294
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000295 arrayBoundsClamper.SetClampingStrategy(resources.ArrayIndexClampingStrategy);
296 clampingStrategy = resources.ArrayIndexClampingStrategy;
297
daniel@transgaming.comc23f4612012-11-28 19:42:57 +0000298 hashFunction = resources.HashFunction;
299
alokp@chromium.org07620a52010-09-23 17:53:56 +0000300 return true;
301}
302
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100303TIntermBlock *TCompiler::compileTreeForTesting(const char *const shaderStrings[],
304 size_t numStrings,
305 ShCompileOptions compileOptions)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000306{
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200307 return compileTreeImpl(shaderStrings, numStrings, compileOptions);
308}
309
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100310TIntermBlock *TCompiler::compileTreeImpl(const char *const shaderStrings[],
311 size_t numStrings,
312 const ShCompileOptions compileOptions)
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200313{
alokp@chromium.org07620a52010-09-23 17:53:56 +0000314 clearResults();
315
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200316 ASSERT(numStrings > 0);
317 ASSERT(GetGlobalPoolAllocator());
alokp@chromium.org07620a52010-09-23 17:53:56 +0000318
David Yen0fbd1282015-02-02 14:46:09 -0800319 // Reset the extension behavior for each compilation unit.
320 ResetExtensionBehavior(extensionBehavior);
321
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000322 // First string is path of source file if flag is set. The actual source follows.
shannon.woods@transgaming.comd64b3da2013-02-28 23:19:26 +0000323 size_t firstSource = 0;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000324 if (compileOptions & SH_SOURCE_PATH)
325 {
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200326 mSourcePath = shaderStrings[0];
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000327 ++firstSource;
328 }
329
Olli Etuahof119a262016-08-19 15:54:22 +0300330 TParseContext parseContext(symbolTable, extensionBehavior, shaderType, shaderSpec,
Olli Etuaho77ba4082016-12-16 12:01:18 +0000331 compileOptions, true, &mDiagnostics, getResources());
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200332
Olli Etuahoa6996682015-10-12 14:32:30 +0300333 parseContext.setFragmentPrecisionHighOnESSL1(fragmentPrecisionHigh);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000334
335 // We preserve symbols at the built-in level from compile-to-compile.
336 // Start pushing the user-defined symbols at global level.
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400337 TScopedSymbolTableLevel scopedSymbolLevel(&symbolTable);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000338
339 // Parse shader.
Olli Etuahod10cf692017-11-02 11:06:14 +0200340 if (PaParseStrings(numStrings - firstSource, &shaderStrings[firstSource], nullptr,
341 &parseContext) != 0)
342 {
343 return nullptr;
344 }
shannon.woods%transgaming.com@gtempaccount.com0bbed382013-04-13 03:38:07 +0000345
Olli Etuahod10cf692017-11-02 11:06:14 +0200346 if (parseContext.getTreeRoot() == nullptr)
347 {
348 return nullptr;
349 }
350
351 setASTMetadata(parseContext);
352
353 if (MapSpecToShaderVersion(shaderSpec) < shaderVersion)
Zhenyao Modb9b40b2014-10-29 15:00:04 -0700354 {
Olli Etuaho77ba4082016-12-16 12:01:18 +0000355 mDiagnostics.globalError("unsupported shader version");
Olli Etuahod10cf692017-11-02 11:06:14 +0200356 return nullptr;
Zhenyao Modb9b40b2014-10-29 15:00:04 -0700357 }
shannon.woods%transgaming.com@gtempaccount.com0bbed382013-04-13 03:38:07 +0000358
Olli Etuahod10cf692017-11-02 11:06:14 +0200359 TIntermBlock *root = parseContext.getTreeRoot();
360 if (!checkAndSimplifyAST(root, parseContext, compileOptions))
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800361 {
Olli Etuahod10cf692017-11-02 11:06:14 +0200362 return nullptr;
363 }
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700364
Olli Etuahod10cf692017-11-02 11:06:14 +0200365 return root;
366}
Martin Radev802abe02016-08-04 17:48:32 +0300367
Olli Etuahod10cf692017-11-02 11:06:14 +0200368void TCompiler::setASTMetadata(const TParseContext &parseContext)
369{
370 shaderVersion = parseContext.getShaderVersion();
Olli Etuaho09b04a22016-12-15 13:30:26 +0000371
Olli Etuahod10cf692017-11-02 11:06:14 +0200372 mPragma = parseContext.pragma();
373 symbolTable.setGlobalInvariant(mPragma.stdgl.invariantAll);
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000374
Olli Etuahod10cf692017-11-02 11:06:14 +0200375 mComputeShaderLocalSizeDeclared = parseContext.isComputeShaderLocalSizeDeclared();
376 mComputeShaderLocalSize = parseContext.getComputeShaderLocalSize();
Olli Etuahoa6996682015-10-12 14:32:30 +0300377
Olli Etuahod10cf692017-11-02 11:06:14 +0200378 mNumViews = parseContext.getNumViews();
379
380 // Highp might have been auto-enabled based on shader version
381 fragmentPrecisionHigh = parseContext.getFragmentPrecisionHigh();
382
383 if (shaderType == GL_GEOMETRY_SHADER_OES)
384 {
385 mGeometryShaderInputPrimitiveType = parseContext.getGeometryShaderInputPrimitiveType();
386 mGeometryShaderOutputPrimitiveType = parseContext.getGeometryShaderOutputPrimitiveType();
387 mGeometryShaderMaxVertices = parseContext.getGeometryShaderMaxVertices();
388 mGeometryShaderInvocations = parseContext.getGeometryShaderInvocations();
389 }
390}
391
392bool TCompiler::checkAndSimplifyAST(TIntermBlock *root,
393 const TParseContext &parseContext,
394 ShCompileOptions compileOptions)
395{
396 // Disallow expressions deemed too complex.
397 if ((compileOptions & SH_LIMIT_EXPRESSION_COMPLEXITY) && !limitExpressionComplexity(root))
398 {
399 return false;
400 }
401
402 // We prune no-ops to work around driver bugs and to keep AST processing and output simple.
403 // The following kinds of no-ops are pruned:
404 // 1. Empty declarations "int;".
405 // 2. Literal statements: "1.0;". The ESSL output doesn't define a default precision
406 // for float, so float literal statements would end up with no precision which is
407 // invalid ESSL.
408 // After this empty declarations are not allowed in the AST.
409 PruneNoOps(root);
410
411 // In case the last case inside a switch statement is a certain type of no-op, GLSL
412 // compilers in drivers may not accept it. In this case we clean up the dead code from the
413 // end of switch statements. This is also required because PruneNoOps may have left switch
414 // statements that only contained an empty declaration inside the final case in an invalid
415 // state. Relies on that PruneNoOps has already been run.
416 RemoveNoOpCasesFromEndOfSwitchStatements(root, &symbolTable);
417
418 // Remove empty switch statements - this makes output simpler.
419 RemoveEmptySwitchStatements(root);
420
421 // Create the function DAG and check there is no recursion
422 if (!initCallDag(root))
423 {
424 return false;
425 }
426
427 if ((compileOptions & SH_LIMIT_CALL_STACK_DEPTH) && !checkCallDepth())
428 {
429 return false;
430 }
431
432 // Checks which functions are used and if "main" exists
433 functionMetadata.clear();
434 functionMetadata.resize(mCallDag.size());
435 if (!tagUsedFunctions())
436 {
437 return false;
438 }
439
440 if (!(compileOptions & SH_DONT_PRUNE_UNUSED_FUNCTIONS))
441 {
442 pruneUnusedFunctions(root);
443 }
444
445 if (shaderVersion >= 310 && !ValidateVaryingLocations(root, &mDiagnostics, shaderType))
446 {
447 return false;
448 }
449
450 if (shaderVersion >= 300 && shaderType == GL_FRAGMENT_SHADER &&
451 !ValidateOutputs(root, getExtensionBehavior(), compileResources.MaxDrawBuffers,
452 &mDiagnostics))
453 {
454 return false;
455 }
456
457 if (shouldRunLoopAndIndexingValidation(compileOptions) &&
458 !ValidateLimitations(root, shaderType, &symbolTable, shaderVersion, &mDiagnostics))
459 {
460 return false;
461 }
462
463 // Fail compilation if precision emulation not supported.
464 if (getResources().WEBGL_debug_shader_precision && getPragma().debugShaderPrecision &&
465 !EmulatePrecision::SupportedInLanguage(outputType))
466 {
467 mDiagnostics.globalError("Precision emulation not supported for this output type.");
468 return false;
469 }
470
Olli Etuahod10cf692017-11-02 11:06:14 +0200471 // Clamping uniform array bounds needs to happen after validateLimitations pass.
472 if (compileOptions & SH_CLAMP_INDIRECT_ARRAY_BOUNDS)
473 {
474 arrayBoundsClamper.MarkIndirectArrayBoundsForClamping(root);
475 }
476
477 if ((compileOptions & SH_INITIALIZE_BUILTINS_FOR_INSTANCED_MULTIVIEW) &&
478 parseContext.isExtensionEnabled(TExtension::OVR_multiview) &&
479 getShaderType() != GL_COMPUTE_SHADER)
480 {
481 DeclareAndInitBuiltinsForInstancedMultiview(root, mNumViews, shaderType, compileOptions,
482 outputType, &symbolTable);
483 }
484
485 // This pass might emit short circuits so keep it before the short circuit unfolding
486 if (compileOptions & SH_REWRITE_DO_WHILE_LOOPS)
487 RewriteDoWhile(root, &symbolTable);
488
489 if (compileOptions & SH_ADD_AND_TRUE_TO_LOOP_CONDITION)
490 sh::AddAndTrueToLoopCondition(root);
491
492 if (compileOptions & SH_UNFOLD_SHORT_CIRCUIT)
493 {
494 UnfoldShortCircuitAST unfoldShortCircuit;
495 root->traverse(&unfoldShortCircuit);
496 unfoldShortCircuit.updateTree();
497 }
498
499 if (compileOptions & SH_REMOVE_POW_WITH_CONSTANT_EXPONENT)
500 {
501 RemovePow(root);
502 }
503
Olli Etuahod10cf692017-11-02 11:06:14 +0200504 if (compileOptions & SH_REGENERATE_STRUCT_NAMES)
505 {
Olli Etuaho9d4d7f02017-12-07 17:11:41 +0100506 RegenerateStructNames gen(&symbolTable);
Olli Etuahod10cf692017-11-02 11:06:14 +0200507 root->traverse(&gen);
508 }
509
510 if (shaderType == GL_FRAGMENT_SHADER && shaderVersion == 100 &&
511 compileResources.EXT_draw_buffers && compileResources.MaxDrawBuffers > 1 &&
512 IsExtensionEnabled(extensionBehavior, TExtension::EXT_draw_buffers))
513 {
514 EmulateGLFragColorBroadcast(root, compileResources.MaxDrawBuffers, &outputVariables,
515 &symbolTable, shaderVersion);
516 }
517
Olli Etuahoae04e1e2017-11-27 16:00:39 +0200518 int simplifyScalarized = (compileOptions & SH_SCALARIZE_VEC_AND_MAT_CONSTRUCTOR_ARGS)
519 ? IntermNodePatternMatcher::kScalarizedVecOrMatConstructor
520 : 0;
521
Olli Etuahod10cf692017-11-02 11:06:14 +0200522 // Split multi declarations and remove calls to array length().
523 // Note that SimplifyLoopConditions needs to be run before any other AST transformations
524 // that may need to generate new statements from loop conditions or loop expressions.
Olli Etuahoae04e1e2017-11-27 16:00:39 +0200525 SimplifyLoopConditions(root,
526 IntermNodePatternMatcher::kMultiDeclaration |
527 IntermNodePatternMatcher::kArrayLengthMethod | simplifyScalarized,
528 &getSymbolTable(), getShaderVersion());
Olli Etuahod10cf692017-11-02 11:06:14 +0200529
530 // Note that separate declarations need to be run before other AST transformations that
531 // generate new statements from expressions.
532 SeparateDeclarations(root);
533
Olli Etuahoae04e1e2017-11-27 16:00:39 +0200534 SplitSequenceOperator(root, IntermNodePatternMatcher::kArrayLengthMethod | simplifyScalarized,
535 &getSymbolTable(), getShaderVersion());
Olli Etuahod10cf692017-11-02 11:06:14 +0200536
537 RemoveArrayLengthMethod(root);
538
Olli Etuaho3d70ca92017-11-10 16:53:26 +0200539 RemoveUnreferencedVariables(root, &symbolTable);
540
541 // Built-in function emulation needs to happen after validateLimitations pass.
542 // TODO(jmadill): Remove global pool allocator.
543 GetGlobalPoolAllocator()->lock();
544 initBuiltInFunctionEmulator(&builtInFunctionEmulator, compileOptions);
545 GetGlobalPoolAllocator()->unlock();
546 builtInFunctionEmulator.markBuiltInFunctionsForEmulation(root);
547
548 if (compileOptions & SH_SCALARIZE_VEC_AND_MAT_CONSTRUCTOR_ARGS)
549 {
550 ScalarizeVecAndMatConstructorArgs(root, shaderType, fragmentPrecisionHigh, &symbolTable);
551 }
552
553 if (shouldCollectVariables(compileOptions))
554 {
555 ASSERT(!variablesCollected);
556 CollectVariables(root, &attributes, &outputVariables, &uniforms, &inputVaryings,
557 &outputVaryings, &uniformBlocks, &shaderStorageBlocks, &inBlocks,
558 hashFunction, &symbolTable, shaderVersion, shaderType, extensionBehavior);
559 collectInterfaceBlocks();
560 variablesCollected = true;
561 if (compileOptions & SH_USE_UNUSED_STANDARD_SHARED_BLOCKS)
562 {
563 useAllMembersInUnusedStandardAndSharedBlocks(root);
564 }
565 if (compileOptions & SH_ENFORCE_PACKING_RESTRICTIONS)
566 {
567 // Returns true if, after applying the packing rules in the GLSL ES 1.00.17 spec
568 // Appendix A, section 7, the shader does not use too many uniforms.
569 if (!CheckVariablesInPackingLimits(maxUniformVectors, uniforms))
570 {
571 mDiagnostics.globalError("too many uniforms");
572 return false;
573 }
574 }
575 if (compileOptions & SH_INIT_OUTPUT_VARIABLES)
576 {
577 initializeOutputVariables(root);
578 }
579 }
580
581 // Removing invariant declarations must be done after collecting variables.
582 // Otherwise, built-in invariant declarations don't apply.
583 if (RemoveInvariant(shaderType, shaderVersion, outputType, compileOptions))
584 {
585 RemoveInvariantDeclaration(root);
586 }
587
588 // gl_Position is always written in compatibility output mode.
589 // It may have been already initialized among other output variables, in that case we don't
590 // need to initialize it twice.
591 if (shaderType == GL_VERTEX_SHADER && !mGLPositionInitialized &&
592 ((compileOptions & SH_INIT_GL_POSITION) || (outputType == SH_GLSL_COMPATIBILITY_OUTPUT)))
593 {
594 initializeGLPosition(root);
595 mGLPositionInitialized = true;
596 }
597
Olli Etuahob5601eb2017-11-15 18:08:04 +0200598 // DeferGlobalInitializers needs to be run before other AST transformations that generate new
599 // statements from expressions. But it's fine to run DeferGlobalInitializers after the above
600 // SplitSequenceOperator and RemoveArrayLengthMethod since they only have an effect on the AST
601 // on ESSL >= 3.00, and the initializers that need to be deferred can only exist in ESSL < 3.00.
602 bool initializeLocalsAndGlobals =
603 (compileOptions & SH_INITIALIZE_UNINITIALIZED_LOCALS) && !IsOutputHLSL(getOutputType());
Olli Etuaho2c7f34c2017-10-09 17:18:02 +0300604 bool canUseLoopsToInitialize = !(compileOptions & SH_DONT_USE_LOOPS_TO_INITIALIZE_VARIABLES);
Olli Etuaho87cc90d2017-12-12 15:28:06 +0200605 bool highPrecisionSupported =
606 shaderType != GL_FRAGMENT_SHADER || compileResources.FragmentPrecisionHigh;
607 DeferGlobalInitializers(root, initializeLocalsAndGlobals, canUseLoopsToInitialize,
608 highPrecisionSupported, &symbolTable);
Olli Etuaho2c7f34c2017-10-09 17:18:02 +0300609
Olli Etuahob5601eb2017-11-15 18:08:04 +0200610 if (initializeLocalsAndGlobals)
Olli Etuahod10cf692017-11-02 11:06:14 +0200611 {
612 // Initialize uninitialized local variables.
613 // In some cases initializing can generate extra statements in the parent block, such as
614 // when initializing nameless structs or initializing arrays in ESSL 1.00. In that case
615 // we need to first simplify loop conditions. We've already separated declarations
616 // earlier, which is also required. If we don't follow the Appendix A limitations, loop
617 // init statements can declare arrays or nameless structs and have multiple
618 // declarations.
619
620 if (!shouldRunLoopAndIndexingValidation(compileOptions))
621 {
622 SimplifyLoopConditions(root,
623 IntermNodePatternMatcher::kArrayDeclaration |
624 IntermNodePatternMatcher::kNamelessStructDeclaration,
625 &getSymbolTable(), getShaderVersion());
626 }
Olli Etuaho661fc482017-10-16 12:17:05 +0300627
Olli Etuaho2c7f34c2017-10-09 17:18:02 +0300628 InitializeUninitializedLocals(root, getShaderVersion(), canUseLoopsToInitialize,
Olli Etuaho87cc90d2017-12-12 15:28:06 +0200629 highPrecisionSupported, &getSymbolTable());
Olli Etuahod10cf692017-11-02 11:06:14 +0200630 }
631
632 if (getShaderType() == GL_VERTEX_SHADER && (compileOptions & SH_CLAMP_POINT_SIZE))
633 {
634 ClampPointSize(root, compileResources.MaxPointSize, &getSymbolTable());
635 }
636
Olli Etuaho661fc482017-10-16 12:17:05 +0300637 if (compileOptions & SH_REWRITE_VECTOR_SCALAR_ARITHMETIC)
638 {
639 VectorizeVectorScalarArithmetic(root, &getSymbolTable());
640 }
641
Olli Etuahod10cf692017-11-02 11:06:14 +0200642 return true;
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200643}
644
Qiankun Miao7ebb97f2016-09-08 18:01:50 +0800645bool TCompiler::compile(const char *const shaderStrings[],
646 size_t numStrings,
647 ShCompileOptions compileOptionsIn)
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200648{
Corentin Wallez28b65282016-06-16 07:24:50 -0700649#if defined(ANGLE_ENABLE_FUZZER_CORPUS_OUTPUT)
650 DumpFuzzerCase(shaderStrings, numStrings, shaderType, shaderSpec, outputType, compileOptionsIn);
651#endif // defined(ANGLE_ENABLE_FUZZER_CORPUS_OUTPUT)
652
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200653 if (numStrings == 0)
654 return true;
655
Qiankun Miao7ebb97f2016-09-08 18:01:50 +0800656 ShCompileOptions compileOptions = compileOptionsIn;
Kenneth Russellbccc65d2016-07-19 16:48:43 -0700657
658 // Apply key workarounds.
659 if (shouldFlattenPragmaStdglInvariantAll())
660 {
661 // This should be harmless to do in all cases, but for the moment, do it only conditionally.
662 compileOptions |= SH_FLATTEN_PRAGMA_STDGL_INVARIANT_ALL;
663 }
664
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200665 TScopedPoolAllocator scopedAlloc(&allocator);
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100666 TIntermBlock *root = compileTreeImpl(shaderStrings, numStrings, compileOptions);
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200667
668 if (root)
669 {
670 if (compileOptions & SH_INTERMEDIATE_TREE)
Olli Etuahocccf2b02017-07-05 14:50:54 +0300671 OutputTree(root, infoSink.info);
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200672
673 if (compileOptions & SH_OBJECT_CODE)
Olli Etuaho89a69a02017-10-23 12:20:45 +0300674 {
675 PerformanceDiagnostics perfDiagnostics(&mDiagnostics);
676 translate(root, compileOptions, &perfDiagnostics);
677 }
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200678
679 // The IntermNode tree doesn't need to be deleted here, since the
680 // memory will be freed in a big chunk by the PoolAllocator.
681 return true;
682 }
683 return false;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000684}
685
Nicolas Capens49a88872013-06-20 09:54:03 -0400686bool TCompiler::InitBuiltInSymbolTable(const ShBuiltInResources &resources)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000687{
Olli Etuaho28cb0362016-11-22 15:42:37 +0000688 if (resources.MaxDrawBuffers < 1)
689 {
690 return false;
691 }
692 if (resources.EXT_blend_func_extended && resources.MaxDualSourceDrawBuffers < 1)
693 {
694 return false;
695 }
696
shannon.woods%transgaming.com@gtempaccount.com18b4c4b2013-04-13 03:31:40 +0000697 compileResources = resources;
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400698 setResourceString();
shannonwoods@chromium.org2ac0be92013-05-30 00:02:27 +0000699
Olli Etuaho977ee7e2017-07-21 11:38:27 +0300700 ASSERT(symbolTable.isEmpty());
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500701 symbolTable.push(); // COMMON_BUILTINS
702 symbolTable.push(); // ESSL1_BUILTINS
703 symbolTable.push(); // ESSL3_BUILTINS
704 symbolTable.push(); // ESSL3_1_BUILTINS
Olli Etuaho977ee7e2017-07-21 11:38:27 +0300705 symbolTable.push(); // GLSL_BUILTINS
shannonwoods@chromium.org2ac0be92013-05-30 00:02:27 +0000706
Jamie Madillacb4b812016-11-07 13:50:29 -0500707 switch (shaderType)
Nicolas Capens49a88872013-06-20 09:54:03 -0400708 {
Jamie Madillacb4b812016-11-07 13:50:29 -0500709 case GL_FRAGMENT_SHADER:
Olli Etuahocce89652017-06-19 16:04:09 +0300710 symbolTable.setDefaultPrecision(EbtInt, EbpMedium);
Jamie Madillacb4b812016-11-07 13:50:29 -0500711 break;
712 case GL_VERTEX_SHADER:
Jamie Madillacb4b812016-11-07 13:50:29 -0500713 case GL_COMPUTE_SHADER:
Shaob5cc1192017-07-06 10:47:20 +0800714 case GL_GEOMETRY_SHADER_OES:
Olli Etuahocce89652017-06-19 16:04:09 +0300715 symbolTable.setDefaultPrecision(EbtInt, EbpHigh);
716 symbolTable.setDefaultPrecision(EbtFloat, EbpHigh);
Jamie Madillacb4b812016-11-07 13:50:29 -0500717 break;
718 default:
Shaob5cc1192017-07-06 10:47:20 +0800719 UNREACHABLE();
Nicolas Capens49a88872013-06-20 09:54:03 -0400720 }
Olli Etuaho183d7e22015-11-20 15:59:09 +0200721 // Set defaults for sampler types that have default precision, even those that are
Zhenyao Moa5a1dfc2013-09-23 14:57:03 -0400722 // only available if an extension exists.
Olli Etuaho183d7e22015-11-20 15:59:09 +0200723 // New sampler types in ESSL3 don't have default precision. ESSL1 types do.
724 initSamplerDefaultPrecision(EbtSampler2D);
725 initSamplerDefaultPrecision(EbtSamplerCube);
726 // SamplerExternalOES is specified in the extension to have default precision.
727 initSamplerDefaultPrecision(EbtSamplerExternalOES);
Andrei Volykhina5527072017-03-22 16:46:30 +0300728 // SamplerExternal2DY2YEXT is specified in the extension to have default precision.
729 initSamplerDefaultPrecision(EbtSamplerExternal2DY2YEXT);
Olli Etuaho183d7e22015-11-20 15:59:09 +0200730 // It isn't specified whether Sampler2DRect has default precision.
731 initSamplerDefaultPrecision(EbtSampler2DRect);
Nicolas Capens49a88872013-06-20 09:54:03 -0400732
Olli Etuahocce89652017-06-19 16:04:09 +0300733 symbolTable.setDefaultPrecision(EbtAtomicCounter, EbpHigh);
jchen104cdac9e2017-05-08 11:01:20 +0800734
Jamie Madill1b452142013-07-12 14:51:11 -0400735 InsertBuiltInFunctions(shaderType, shaderSpec, resources, symbolTable);
Nicolas Capens49a88872013-06-20 09:54:03 -0400736
737 IdentifyBuiltIns(shaderType, shaderSpec, resources, symbolTable);
738
Olli Etuaho5d69db12017-11-24 16:51:15 +0200739 symbolTable.markBuiltInInitializationFinished();
740
Nicolas Capens49a88872013-06-20 09:54:03 -0400741 return true;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000742}
743
Olli Etuaho183d7e22015-11-20 15:59:09 +0200744void TCompiler::initSamplerDefaultPrecision(TBasicType samplerType)
745{
746 ASSERT(samplerType > EbtGuardSamplerBegin && samplerType < EbtGuardSamplerEnd);
Olli Etuahocce89652017-06-19 16:04:09 +0300747 symbolTable.setDefaultPrecision(samplerType, EbpLow);
Olli Etuaho183d7e22015-11-20 15:59:09 +0200748}
749
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400750void TCompiler::setResourceString()
751{
752 std::ostringstream strstream;
Geoff Langb66a9092016-05-16 15:59:14 -0400753
754 // clang-format off
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400755 strstream << ":MaxVertexAttribs:" << compileResources.MaxVertexAttribs
Jamie Madillacb4b812016-11-07 13:50:29 -0500756 << ":MaxVertexUniformVectors:" << compileResources.MaxVertexUniformVectors
757 << ":MaxVaryingVectors:" << compileResources.MaxVaryingVectors
758 << ":MaxVertexTextureImageUnits:" << compileResources.MaxVertexTextureImageUnits
759 << ":MaxCombinedTextureImageUnits:" << compileResources.MaxCombinedTextureImageUnits
760 << ":MaxTextureImageUnits:" << compileResources.MaxTextureImageUnits
761 << ":MaxFragmentUniformVectors:" << compileResources.MaxFragmentUniformVectors
762 << ":MaxDrawBuffers:" << compileResources.MaxDrawBuffers
763 << ":OES_standard_derivatives:" << compileResources.OES_standard_derivatives
764 << ":OES_EGL_image_external:" << compileResources.OES_EGL_image_external
765 << ":OES_EGL_image_external_essl3:" << compileResources.OES_EGL_image_external_essl3
766 << ":NV_EGL_stream_consumer_external:" << compileResources.NV_EGL_stream_consumer_external
767 << ":ARB_texture_rectangle:" << compileResources.ARB_texture_rectangle
768 << ":EXT_draw_buffers:" << compileResources.EXT_draw_buffers
769 << ":FragmentPrecisionHigh:" << compileResources.FragmentPrecisionHigh
770 << ":MaxExpressionComplexity:" << compileResources.MaxExpressionComplexity
771 << ":MaxCallStackDepth:" << compileResources.MaxCallStackDepth
772 << ":MaxFunctionParameters:" << compileResources.MaxFunctionParameters
773 << ":EXT_blend_func_extended:" << compileResources.EXT_blend_func_extended
774 << ":EXT_frag_depth:" << compileResources.EXT_frag_depth
775 << ":EXT_shader_texture_lod:" << compileResources.EXT_shader_texture_lod
776 << ":EXT_shader_framebuffer_fetch:" << compileResources.EXT_shader_framebuffer_fetch
777 << ":NV_shader_framebuffer_fetch:" << compileResources.NV_shader_framebuffer_fetch
778 << ":ARM_shader_framebuffer_fetch:" << compileResources.ARM_shader_framebuffer_fetch
Martin Radev318f9aa2017-05-17 17:47:28 +0300779 << ":OVR_multiview:" << compileResources.OVR_multiview
Andrei Volykhina5527072017-03-22 16:46:30 +0300780 << ":EXT_YUV_target:" << compileResources.EXT_YUV_target
Shaob5cc1192017-07-06 10:47:20 +0800781 << ":OES_geometry_shader:" << compileResources.OES_geometry_shader
Jamie Madillacb4b812016-11-07 13:50:29 -0500782 << ":MaxVertexOutputVectors:" << compileResources.MaxVertexOutputVectors
783 << ":MaxFragmentInputVectors:" << compileResources.MaxFragmentInputVectors
784 << ":MinProgramTexelOffset:" << compileResources.MinProgramTexelOffset
785 << ":MaxProgramTexelOffset:" << compileResources.MaxProgramTexelOffset
786 << ":MaxDualSourceDrawBuffers:" << compileResources.MaxDualSourceDrawBuffers
Martin Radev318f9aa2017-05-17 17:47:28 +0300787 << ":MaxViewsOVR:" << compileResources.MaxViewsOVR
Jamie Madillacb4b812016-11-07 13:50:29 -0500788 << ":NV_draw_buffers:" << compileResources.NV_draw_buffers
789 << ":WEBGL_debug_shader_precision:" << compileResources.WEBGL_debug_shader_precision
Martin Radev84aa2dc2017-09-11 15:51:02 +0300790 << ":MinProgramTextureGatherOffset:" << compileResources.MinProgramTextureGatherOffset
791 << ":MaxProgramTextureGatherOffset:" << compileResources.MaxProgramTextureGatherOffset
Jamie Madillacb4b812016-11-07 13:50:29 -0500792 << ":MaxImageUnits:" << compileResources.MaxImageUnits
793 << ":MaxVertexImageUniforms:" << compileResources.MaxVertexImageUniforms
794 << ":MaxFragmentImageUniforms:" << compileResources.MaxFragmentImageUniforms
795 << ":MaxComputeImageUniforms:" << compileResources.MaxComputeImageUniforms
796 << ":MaxCombinedImageUniforms:" << compileResources.MaxCombinedImageUniforms
797 << ":MaxCombinedShaderOutputResources:" << compileResources.MaxCombinedShaderOutputResources
798 << ":MaxComputeWorkGroupCountX:" << compileResources.MaxComputeWorkGroupCount[0]
799 << ":MaxComputeWorkGroupCountY:" << compileResources.MaxComputeWorkGroupCount[1]
800 << ":MaxComputeWorkGroupCountZ:" << compileResources.MaxComputeWorkGroupCount[2]
801 << ":MaxComputeWorkGroupSizeX:" << compileResources.MaxComputeWorkGroupSize[0]
802 << ":MaxComputeWorkGroupSizeY:" << compileResources.MaxComputeWorkGroupSize[1]
803 << ":MaxComputeWorkGroupSizeZ:" << compileResources.MaxComputeWorkGroupSize[2]
804 << ":MaxComputeUniformComponents:" << compileResources.MaxComputeUniformComponents
805 << ":MaxComputeTextureImageUnits:" << compileResources.MaxComputeTextureImageUnits
806 << ":MaxComputeAtomicCounters:" << compileResources.MaxComputeAtomicCounters
807 << ":MaxComputeAtomicCounterBuffers:" << compileResources.MaxComputeAtomicCounterBuffers
808 << ":MaxVertexAtomicCounters:" << compileResources.MaxVertexAtomicCounters
809 << ":MaxFragmentAtomicCounters:" << compileResources.MaxFragmentAtomicCounters
810 << ":MaxCombinedAtomicCounters:" << compileResources.MaxCombinedAtomicCounters
811 << ":MaxAtomicCounterBindings:" << compileResources.MaxAtomicCounterBindings
812 << ":MaxVertexAtomicCounterBuffers:" << compileResources.MaxVertexAtomicCounterBuffers
813 << ":MaxFragmentAtomicCounterBuffers:" << compileResources.MaxFragmentAtomicCounterBuffers
814 << ":MaxCombinedAtomicCounterBuffers:" << compileResources.MaxCombinedAtomicCounterBuffers
Shaob5cc1192017-07-06 10:47:20 +0800815 << ":MaxAtomicCounterBufferSize:" << compileResources.MaxAtomicCounterBufferSize
Shaob5cc1192017-07-06 10:47:20 +0800816 << ":MaxGeometryUniformComponents:" << compileResources.MaxGeometryUniformComponents
Jiawei Shaod27f5c82017-08-23 09:38:08 +0800817 << ":MaxGeometryUniformBlocks:" << compileResources.MaxGeometryUniformBlocks
818 << ":MaxGeometryInputComponents:" << compileResources.MaxGeometryInputComponents
819 << ":MaxGeometryOutputComponents:" << compileResources.MaxGeometryOutputComponents
820 << ":MaxGeometryOutputVertices:" << compileResources.MaxGeometryOutputVertices
821 << ":MaxGeometryTotalOutputComponents:" << compileResources.MaxGeometryTotalOutputComponents
822 << ":MaxGeometryTextureImageUnits:" << compileResources.MaxGeometryTextureImageUnits
823 << ":MaxGeometryAtomicCounterBuffers:" << compileResources.MaxGeometryAtomicCounterBuffers
824 << ":MaxGeometryAtomicCounters:" << compileResources.MaxGeometryAtomicCounters
825 << ":MaxGeometryShaderStorageBlocks:" << compileResources.MaxGeometryShaderStorageBlocks
826 << ":MaxGeometryShaderInvocations:" << compileResources.MaxGeometryShaderInvocations
827 << ":MaxGeometryImageUniforms:" << compileResources.MaxGeometryImageUniforms;
Geoff Langb66a9092016-05-16 15:59:14 -0400828 // clang-format on
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400829
830 builtInResourcesString = strstream.str();
831}
832
Jiajia Qin9b11ea42017-07-11 16:50:08 +0800833void TCompiler::collectInterfaceBlocks()
834{
835 ASSERT(interfaceBlocks.empty());
Jiawei Shaod8105a02017-08-08 09:54:36 +0800836 interfaceBlocks.reserve(uniformBlocks.size() + shaderStorageBlocks.size() + inBlocks.size());
Jiajia Qin9b11ea42017-07-11 16:50:08 +0800837 interfaceBlocks.insert(interfaceBlocks.end(), uniformBlocks.begin(), uniformBlocks.end());
838 interfaceBlocks.insert(interfaceBlocks.end(), shaderStorageBlocks.begin(),
839 shaderStorageBlocks.end());
Jiawei Shaod8105a02017-08-08 09:54:36 +0800840 interfaceBlocks.insert(interfaceBlocks.end(), inBlocks.begin(), inBlocks.end());
Jiajia Qin9b11ea42017-07-11 16:50:08 +0800841}
842
alokp@chromium.org07620a52010-09-23 17:53:56 +0000843void TCompiler::clearResults()
844{
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000845 arrayBoundsClamper.Cleanup();
alokp@chromium.org07620a52010-09-23 17:53:56 +0000846 infoSink.info.erase();
847 infoSink.obj.erase();
848 infoSink.debug.erase();
Olli Etuaho77ba4082016-12-16 12:01:18 +0000849 mDiagnostics.resetErrorCount();
alokp@chromium.org07620a52010-09-23 17:53:56 +0000850
Jamie Madilled27c722014-07-02 15:31:23 -0400851 attributes.clear();
852 outputVariables.clear();
alokp@chromium.org07620a52010-09-23 17:53:56 +0000853 uniforms.clear();
Jiawei-Shaodf7d7c92017-07-31 09:34:04 +0800854 inputVaryings.clear();
855 outputVaryings.clear();
Jamie Madilled27c722014-07-02 15:31:23 -0400856 interfaceBlocks.clear();
Jiajia Qin9b11ea42017-07-11 16:50:08 +0800857 uniformBlocks.clear();
858 shaderStorageBlocks.clear();
Jiawei Shaod8105a02017-08-08 09:54:36 +0800859 inBlocks.clear();
Kenneth Russellbccc65d2016-07-19 16:48:43 -0700860 variablesCollected = false;
Olli Etuahob12040c2017-06-27 14:20:45 +0300861 mGLPositionInitialized = false;
zmo@google.coma3b4ab42011-09-16 00:53:26 +0000862
Olli Etuaho09b04a22016-12-15 13:30:26 +0000863 mNumViews = -1;
864
Shaob5cc1192017-07-06 10:47:20 +0800865 mGeometryShaderInputPrimitiveType = EptUndefined;
866 mGeometryShaderOutputPrimitiveType = EptUndefined;
867 mGeometryShaderInvocations = 0;
868 mGeometryShaderMaxVertices = -1;
869
Olli Etuahodfa75e82017-01-23 09:43:06 -0800870 builtInFunctionEmulator.cleanup();
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +0000871
872 nameMap.clear();
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200873
Yunchao Hed7297bf2017-04-19 15:27:10 +0800874 mSourcePath = nullptr;
Olli Etuaho5d69db12017-11-24 16:51:15 +0200875
876 symbolTable.clearCompilationResults();
alokp@chromium.org07620a52010-09-23 17:53:56 +0000877}
878
Corentin Wallez71d147f2015-02-11 11:15:24 -0800879bool TCompiler::initCallDag(TIntermNode *root)
zmo@google.comb1762df2011-07-30 02:04:23 +0000880{
Corentin Wallez71d147f2015-02-11 11:15:24 -0800881 mCallDag.clear();
882
Olli Etuaho77ba4082016-12-16 12:01:18 +0000883 switch (mCallDag.init(root, &mDiagnostics))
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800884 {
Jamie Madillacb4b812016-11-07 13:50:29 -0500885 case CallDAG::INITDAG_SUCCESS:
886 return true;
887 case CallDAG::INITDAG_RECURSION:
Jamie Madillacb4b812016-11-07 13:50:29 -0500888 case CallDAG::INITDAG_UNDEFINED:
Olli Etuaho77ba4082016-12-16 12:01:18 +0000889 // Error message has already been written out.
890 ASSERT(mDiagnostics.numErrors() > 0);
Jamie Madillacb4b812016-11-07 13:50:29 -0500891 return false;
Corentin Wallez71d147f2015-02-11 11:15:24 -0800892 }
893
894 UNREACHABLE();
895 return true;
896}
897
898bool TCompiler::checkCallDepth()
899{
900 std::vector<int> depths(mCallDag.size());
901
902 for (size_t i = 0; i < mCallDag.size(); i++)
903 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500904 int depth = 0;
Corentin Wallez71d147f2015-02-11 11:15:24 -0800905 auto &record = mCallDag.getRecordFromIndex(i);
906
907 for (auto &calleeIndex : record.callees)
908 {
909 depth = std::max(depth, depths[calleeIndex] + 1);
910 }
911
912 depths[i] = depth;
913
914 if (depth >= maxCallStackDepth)
915 {
916 // Trace back the function chain to have a meaningful info log.
Olli Etuaho77ba4082016-12-16 12:01:18 +0000917 std::stringstream errorStream;
918 errorStream << "Call stack too deep (larger than " << maxCallStackDepth
919 << ") with the following call chain: " << record.name;
Corentin Wallez71d147f2015-02-11 11:15:24 -0800920
Cooper Partin4d61f7e2015-08-12 10:56:50 -0700921 int currentFunction = static_cast<int>(i);
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500922 int currentDepth = depth;
Corentin Wallez71d147f2015-02-11 11:15:24 -0800923
924 while (currentFunction != -1)
925 {
Olli Etuaho77ba4082016-12-16 12:01:18 +0000926 errorStream << " -> " << mCallDag.getRecordFromIndex(currentFunction).name;
Corentin Wallez71d147f2015-02-11 11:15:24 -0800927
928 int nextFunction = -1;
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500929 for (auto &calleeIndex : mCallDag.getRecordFromIndex(currentFunction).callees)
Corentin Wallez71d147f2015-02-11 11:15:24 -0800930 {
931 if (depths[calleeIndex] == currentDepth - 1)
932 {
933 currentDepth--;
934 nextFunction = calleeIndex;
935 }
936 }
937
938 currentFunction = nextFunction;
939 }
940
Olli Etuaho77ba4082016-12-16 12:01:18 +0000941 std::string errorStr = errorStream.str();
942 mDiagnostics.globalError(errorStr.c_str());
943
Corentin Wallez71d147f2015-02-11 11:15:24 -0800944 return false;
945 }
946 }
947
948 return true;
949}
950
951bool TCompiler::tagUsedFunctions()
952{
953 // Search from main, starting from the end of the DAG as it usually is the root.
Cooper Partin4d61f7e2015-08-12 10:56:50 -0700954 for (size_t i = mCallDag.size(); i-- > 0;)
Corentin Wallez71d147f2015-02-11 11:15:24 -0800955 {
Olli Etuahoec9232b2017-03-27 17:01:37 +0300956 if (mCallDag.getRecordFromIndex(i).name == "main")
Corentin Wallez71d147f2015-02-11 11:15:24 -0800957 {
958 internalTagUsedFunction(i);
959 return true;
960 }
961 }
962
Olli Etuaho77ba4082016-12-16 12:01:18 +0000963 mDiagnostics.globalError("Missing main()");
Corentin Wallez71d147f2015-02-11 11:15:24 -0800964 return false;
965}
966
967void TCompiler::internalTagUsedFunction(size_t index)
968{
969 if (functionMetadata[index].used)
970 {
971 return;
972 }
973
974 functionMetadata[index].used = true;
975
976 for (int calleeIndex : mCallDag.getRecordFromIndex(index).callees)
977 {
978 internalTagUsedFunction(calleeIndex);
zmo@google.comb1762df2011-07-30 02:04:23 +0000979 }
980}
981
Corentin Walleza094a8a2015-04-07 11:53:06 -0700982// A predicate for the stl that returns if a top-level node is unused
983class TCompiler::UnusedPredicate
984{
985 public:
986 UnusedPredicate(const CallDAG *callDag, const std::vector<FunctionMetadata> *metadatas)
Jamie Madillacb4b812016-11-07 13:50:29 -0500987 : mCallDag(callDag), mMetadatas(metadatas)
Corentin Walleza094a8a2015-04-07 11:53:06 -0700988 {
989 }
990
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500991 bool operator()(TIntermNode *node)
Corentin Walleza094a8a2015-04-07 11:53:06 -0700992 {
Olli Etuaho16c745a2017-01-16 17:02:27 +0000993 const TIntermFunctionPrototype *asFunctionPrototype = node->getAsFunctionPrototypeNode();
994 const TIntermFunctionDefinition *asFunctionDefinition = node->getAsFunctionDefinition();
Corentin Walleza094a8a2015-04-07 11:53:06 -0700995
Olli Etuaho336b1472016-10-05 16:37:55 +0100996 const TFunctionSymbolInfo *functionInfo = nullptr;
997
Olli Etuaho16c745a2017-01-16 17:02:27 +0000998 if (asFunctionDefinition)
Olli Etuaho336b1472016-10-05 16:37:55 +0100999 {
Olli Etuaho16c745a2017-01-16 17:02:27 +00001000 functionInfo = asFunctionDefinition->getFunctionSymbolInfo();
Olli Etuaho336b1472016-10-05 16:37:55 +01001001 }
Olli Etuaho16c745a2017-01-16 17:02:27 +00001002 else if (asFunctionPrototype)
Olli Etuaho336b1472016-10-05 16:37:55 +01001003 {
Olli Etuaho16c745a2017-01-16 17:02:27 +00001004 functionInfo = asFunctionPrototype->getFunctionSymbolInfo();
Olli Etuaho336b1472016-10-05 16:37:55 +01001005 }
1006 if (functionInfo == nullptr)
Corentin Walleza094a8a2015-04-07 11:53:06 -07001007 {
1008 return false;
1009 }
1010
Olli Etuaho336b1472016-10-05 16:37:55 +01001011 size_t callDagIndex = mCallDag->findIndex(functionInfo);
Corentin Walleza094a8a2015-04-07 11:53:06 -07001012 if (callDagIndex == CallDAG::InvalidIndex)
1013 {
1014 // This happens only for unimplemented prototypes which are thus unused
Olli Etuaho16c745a2017-01-16 17:02:27 +00001015 ASSERT(asFunctionPrototype);
Corentin Walleza094a8a2015-04-07 11:53:06 -07001016 return true;
1017 }
1018
1019 ASSERT(callDagIndex < mMetadatas->size());
1020 return !(*mMetadatas)[callDagIndex].used;
1021 }
1022
1023 private:
1024 const CallDAG *mCallDag;
1025 const std::vector<FunctionMetadata> *mMetadatas;
1026};
1027
Olli Etuahod10cf692017-11-02 11:06:14 +02001028void TCompiler::pruneUnusedFunctions(TIntermBlock *root)
Corentin Walleza094a8a2015-04-07 11:53:06 -07001029{
Corentin Walleza094a8a2015-04-07 11:53:06 -07001030 UnusedPredicate isUnused(&mCallDag, &functionMetadata);
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01001031 TIntermSequence *sequence = root->getSequence();
Corentin Wallezb081e782015-07-20 05:40:04 -07001032
1033 if (!sequence->empty())
1034 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001035 sequence->erase(std::remove_if(sequence->begin(), sequence->end(), isUnused),
1036 sequence->end());
Corentin Wallezb081e782015-07-20 05:40:04 -07001037 }
Corentin Walleza094a8a2015-04-07 11:53:06 -07001038}
1039
Olli Etuahob4279202017-05-18 15:08:24 +03001040bool TCompiler::limitExpressionComplexity(TIntermBlock *root)
Jamie Madilleb1a0102013-07-08 13:31:38 -04001041{
Olli Etuahocccf2b02017-07-05 14:50:54 +03001042 if (!IsASTDepthBelowLimit(root, maxExpressionComplexity))
Jamie Madill6654bc92014-03-26 14:01:57 -04001043 {
Olli Etuaho77ba4082016-12-16 12:01:18 +00001044 mDiagnostics.globalError("Expression too complex.");
Jamie Madill6654bc92014-03-26 14:01:57 -04001045 return false;
1046 }
1047
Olli Etuahob4279202017-05-18 15:08:24 +03001048 if (!ValidateMaxParameters(root, maxFunctionParameters))
Olli Etuaho19d1dc92016-03-08 17:18:46 +02001049 {
Olli Etuaho77ba4082016-12-16 12:01:18 +00001050 mDiagnostics.globalError("Function has too many parameters.");
Olli Etuaho19d1dc92016-03-08 17:18:46 +02001051 return false;
1052 }
1053
Jamie Madilleb1a0102013-07-08 13:31:38 -04001054 return true;
1055}
1056
Corentin Wallez1df16022016-10-27 08:16:56 -04001057bool TCompiler::shouldCollectVariables(ShCompileOptions compileOptions)
1058{
1059 return (compileOptions & SH_VARIABLES) != 0;
1060}
1061
1062bool TCompiler::wereVariablesCollected() const
1063{
1064 return variablesCollected;
1065}
1066
Olli Etuaho9cbc07c2017-05-10 18:22:01 +03001067void TCompiler::initializeGLPosition(TIntermBlock *root)
Zhenyao Mo4a667fe2014-02-11 12:35:01 -08001068{
Zhenyao Mo72111912016-07-20 17:45:56 -07001069 InitVariableList list;
Olli Etuahoe7c28572017-10-23 16:29:33 +03001070 sh::ShaderVariable var(GL_FLOAT_VEC4);
Zhenyao Mo72111912016-07-20 17:45:56 -07001071 var.name = "gl_Position";
1072 list.push_back(var);
Olli Etuaho87cc90d2017-12-12 15:28:06 +02001073 InitializeVariables(root, list, &symbolTable, shaderVersion, extensionBehavior, false, false);
Zhenyao Mo4a667fe2014-02-11 12:35:01 -08001074}
1075
Olli Etuaho9cbc07c2017-05-10 18:22:01 +03001076void TCompiler::useAllMembersInUnusedStandardAndSharedBlocks(TIntermBlock *root)
Qin Jiajia7835b522016-10-08 11:20:17 +08001077{
1078 sh::InterfaceBlockList list;
1079
Jiajia Qin9b11ea42017-07-11 16:50:08 +08001080 for (auto block : uniformBlocks)
Qin Jiajia7835b522016-10-08 11:20:17 +08001081 {
1082 if (!block.staticUse &&
Qin Jiajiaca68d982017-09-18 16:41:56 +08001083 (block.layout == sh::BLOCKLAYOUT_STD140 || block.layout == sh::BLOCKLAYOUT_SHARED))
Qin Jiajia7835b522016-10-08 11:20:17 +08001084 {
1085 list.push_back(block);
1086 }
1087 }
1088
Zhenyao Mod7490962016-11-09 15:49:51 -08001089 sh::UseInterfaceBlockFields(root, list, symbolTable);
Qin Jiajia7835b522016-10-08 11:20:17 +08001090}
1091
Olli Etuaho9cbc07c2017-05-10 18:22:01 +03001092void TCompiler::initializeOutputVariables(TIntermBlock *root)
Zhenyao Mo4a667fe2014-02-11 12:35:01 -08001093{
Zhenyao Mo72111912016-07-20 17:45:56 -07001094 InitVariableList list;
Jiawei Shaod27f5c82017-08-23 09:38:08 +08001095 if (shaderType == GL_VERTEX_SHADER || shaderType == GL_GEOMETRY_SHADER_OES)
Zhenyao Mo4a667fe2014-02-11 12:35:01 -08001096 {
Jiawei-Shaodf7d7c92017-07-31 09:34:04 +08001097 for (auto var : outputVaryings)
Zhenyao Mo4a667fe2014-02-11 12:35:01 -08001098 {
Zhenyao Mof9312682016-07-22 12:51:31 -07001099 list.push_back(var);
Olli Etuahob12040c2017-06-27 14:20:45 +03001100 if (var.name == "gl_Position")
1101 {
1102 ASSERT(!mGLPositionInitialized);
1103 mGLPositionInitialized = true;
1104 }
Zhenyao Mo4a667fe2014-02-11 12:35:01 -08001105 }
Zhenyao Mo4a667fe2014-02-11 12:35:01 -08001106 }
Zhenyao Mo72111912016-07-20 17:45:56 -07001107 else
1108 {
1109 ASSERT(shaderType == GL_FRAGMENT_SHADER);
1110 for (auto var : outputVariables)
1111 {
1112 list.push_back(var);
1113 }
1114 }
Olli Etuaho87cc90d2017-12-12 15:28:06 +02001115 InitializeVariables(root, list, &symbolTable, shaderVersion, extensionBehavior, false, false);
Zhenyao Mo4a667fe2014-02-11 12:35:01 -08001116}
1117
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001118const TExtensionBehavior &TCompiler::getExtensionBehavior() const
zmo@google.com5601ea02011-06-10 18:23:25 +00001119{
1120 return extensionBehavior;
1121}
zmo@google.com32e97312011-08-24 01:03:11 +00001122
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02001123const char *TCompiler::getSourcePath() const
1124{
1125 return mSourcePath;
1126}
1127
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001128const ShBuiltInResources &TCompiler::getResources() const
shannon.woods%transgaming.com@gtempaccount.com18b4c4b2013-04-13 03:31:40 +00001129{
1130 return compileResources;
1131}
1132
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001133const ArrayBoundsClamper &TCompiler::getArrayBoundsClamper() const
daniel@transgaming.com4167cc92013-01-11 04:11:53 +00001134{
1135 return arrayBoundsClamper;
1136}
1137
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +00001138ShArrayIndexClampingStrategy TCompiler::getArrayIndexClampingStrategy() const
1139{
1140 return clampingStrategy;
1141}
1142
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001143const BuiltInFunctionEmulator &TCompiler::getBuiltInFunctionEmulator() const
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +00001144{
1145 return builtInFunctionEmulator;
1146}
Zhenyao Mo94ac7b72014-10-15 18:22:08 -07001147
Qiankun Miao7ebb97f2016-09-08 18:01:50 +08001148void TCompiler::writePragma(ShCompileOptions compileOptions)
Zhenyao Mo94ac7b72014-10-15 18:22:08 -07001149{
Kenneth Russellbccc65d2016-07-19 16:48:43 -07001150 if (!(compileOptions & SH_FLATTEN_PRAGMA_STDGL_INVARIANT_ALL))
1151 {
1152 TInfoSinkBase &sink = infoSink.obj;
1153 if (mPragma.stdgl.invariantAll)
1154 sink << "#pragma STDGL invariant(all)\n";
1155 }
1156}
1157
1158bool TCompiler::isVaryingDefined(const char *varyingName)
1159{
1160 ASSERT(variablesCollected);
Jiawei-Shaodf7d7c92017-07-31 09:34:04 +08001161 for (size_t ii = 0; ii < inputVaryings.size(); ++ii)
Kenneth Russellbccc65d2016-07-19 16:48:43 -07001162 {
Jiawei-Shaodf7d7c92017-07-31 09:34:04 +08001163 if (inputVaryings[ii].name == varyingName)
1164 {
1165 return true;
1166 }
1167 }
1168 for (size_t ii = 0; ii < outputVaryings.size(); ++ii)
1169 {
1170 if (outputVaryings[ii].name == varyingName)
Kenneth Russellbccc65d2016-07-19 16:48:43 -07001171 {
1172 return true;
1173 }
1174 }
1175
1176 return false;
Zhenyao Mo94ac7b72014-10-15 18:22:08 -07001177}
Jamie Madillacb4b812016-11-07 13:50:29 -05001178
1179} // namespace sh