blob: 47841710810b736f580cbe9bc16e17d754f4ee9e [file] [log] [blame]
alokp@chromium.org07620a52010-09-23 17:53:56 +00001//
Jamie Madill88f6e942014-02-19 10:27:53 -05002// Copyright (c) 2002-2014 The ANGLE Project Authors. All rights reserved.
alokp@chromium.org07620a52010-09-23 17:53:56 +00003// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
Corentin Wallez8b28a8b2016-09-15 19:47:56 -04007#include "compiler/translator/Compiler.h"
8
9#include <sstream>
10
11#include "angle_gl.h"
12#include "common/utilities.h"
Qiankun Miao09cfac62016-09-06 17:25:16 +080013#include "compiler/translator/AddAndTrueToLoopCondition.h"
Dmitry Skiba01971112015-07-10 14:54:00 -040014#include "compiler/translator/Cache.h"
Corentin Wallez71d147f2015-02-11 11:15:24 -080015#include "compiler/translator/CallDAG.h"
Olli Etuahoab918822017-07-14 17:03:42 +030016#include "compiler/translator/ClampPointSize.h"
Olli Etuaho78ed6cd2017-08-09 16:19:00 +030017#include "compiler/translator/CollectVariables.h"
Martin Radev69056a12017-05-18 11:14:50 +030018#include "compiler/translator/DeclareAndInitBuiltinsForInstancedMultiview.h"
Olli Etuaho3d932d82016-04-12 11:10:30 +030019#include "compiler/translator/DeferGlobalInitializers.h"
Zhenyao Mo4e94fea2016-08-09 14:31:37 -070020#include "compiler/translator/EmulateGLFragColorBroadcast.h"
Jamie Madilld5696192016-10-06 11:09:24 -040021#include "compiler/translator/EmulatePrecision.h"
Geoff Lang17732822013-08-29 13:46:49 -040022#include "compiler/translator/Initialize.h"
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080023#include "compiler/translator/InitializeVariables.h"
Olli Etuaho9733cee2017-05-11 19:14:35 +030024#include "compiler/translator/IntermNodePatternMatcher.h"
Olli Etuahocccf2b02017-07-05 14:50:54 +030025#include "compiler/translator/IsASTDepthBelowLimit.h"
26#include "compiler/translator/OutputTree.h"
Jamie Madill6b9cb252013-10-17 10:45:47 -040027#include "compiler/translator/ParseContext.h"
Olli Etuaho87c35882017-10-19 15:19:38 +030028#include "compiler/translator/PruneNoOps.h"
Zhenyao Moe740add2014-07-18 17:01:01 -070029#include "compiler/translator/RegenerateStructNames.h"
Olli Etuahobb5a7e22017-08-30 13:03:12 +030030#include "compiler/translator/RemoveArrayLengthMethod.h"
Olli Etuahoc1f14fb2017-10-28 19:17:23 +030031#include "compiler/translator/RemoveEmptySwitchStatements.h"
Qiankun Miao705a9192016-08-29 10:05:27 +080032#include "compiler/translator/RemoveInvariantDeclaration.h"
Olli Etuaho923ecef2017-10-11 12:01:38 +030033#include "compiler/translator/RemoveNoOpCasesFromEndOfSwitchStatements.h"
Olli Etuaho5c407bb2015-06-01 12:20:39 +030034#include "compiler/translator/RemovePow.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),
256 mGeometryShaderMaxVertices(-1),
257 mGeometryShaderInvocations(0),
258 mGeometryShaderInputPrimitiveType(EptUndefined),
259 mGeometryShaderOutputPrimitiveType(EptUndefined)
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000260{
Martin Radev802abe02016-08-04 17:48:32 +0300261 mComputeShaderLocalSize.fill(1);
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000262}
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
471 // Built-in function emulation needs to happen after validateLimitations pass.
472 // TODO(jmadill): Remove global pool allocator.
473 GetGlobalPoolAllocator()->lock();
474 initBuiltInFunctionEmulator(&builtInFunctionEmulator, compileOptions);
475 GetGlobalPoolAllocator()->unlock();
476 builtInFunctionEmulator.markBuiltInFunctionsForEmulation(root);
477
478 // Clamping uniform array bounds needs to happen after validateLimitations pass.
479 if (compileOptions & SH_CLAMP_INDIRECT_ARRAY_BOUNDS)
480 {
481 arrayBoundsClamper.MarkIndirectArrayBoundsForClamping(root);
482 }
483
484 if ((compileOptions & SH_INITIALIZE_BUILTINS_FOR_INSTANCED_MULTIVIEW) &&
485 parseContext.isExtensionEnabled(TExtension::OVR_multiview) &&
486 getShaderType() != GL_COMPUTE_SHADER)
487 {
488 DeclareAndInitBuiltinsForInstancedMultiview(root, mNumViews, shaderType, compileOptions,
489 outputType, &symbolTable);
490 }
491
492 // This pass might emit short circuits so keep it before the short circuit unfolding
493 if (compileOptions & SH_REWRITE_DO_WHILE_LOOPS)
494 RewriteDoWhile(root, &symbolTable);
495
496 if (compileOptions & SH_ADD_AND_TRUE_TO_LOOP_CONDITION)
497 sh::AddAndTrueToLoopCondition(root);
498
499 if (compileOptions & SH_UNFOLD_SHORT_CIRCUIT)
500 {
501 UnfoldShortCircuitAST unfoldShortCircuit;
502 root->traverse(&unfoldShortCircuit);
503 unfoldShortCircuit.updateTree();
504 }
505
506 if (compileOptions & SH_REMOVE_POW_WITH_CONSTANT_EXPONENT)
507 {
508 RemovePow(root);
509 }
510
511 if (shouldCollectVariables(compileOptions))
512 {
513 ASSERT(!variablesCollected);
514 CollectVariables(root, &attributes, &outputVariables, &uniforms, &inputVaryings,
515 &outputVaryings, &uniformBlocks, &shaderStorageBlocks, &inBlocks,
516 hashFunction, &symbolTable, shaderVersion, shaderType, extensionBehavior);
517 collectInterfaceBlocks();
518 variablesCollected = true;
519 if (compileOptions & SH_USE_UNUSED_STANDARD_SHARED_BLOCKS)
Shaob5cc1192017-07-06 10:47:20 +0800520 {
Olli Etuahod10cf692017-11-02 11:06:14 +0200521 useAllMembersInUnusedStandardAndSharedBlocks(root);
Shaob5cc1192017-07-06 10:47:20 +0800522 }
Olli Etuahod10cf692017-11-02 11:06:14 +0200523 if (compileOptions & SH_ENFORCE_PACKING_RESTRICTIONS)
Corentin Wallez71d147f2015-02-11 11:15:24 -0800524 {
Olli Etuahod10cf692017-11-02 11:06:14 +0200525 // Returns true if, after applying the packing rules in the GLSL ES 1.00.17 spec
526 // Appendix A, section 7, the shader does not use too many uniforms.
527 if (!CheckVariablesInPackingLimits(maxUniformVectors, uniforms))
Jamie Madilld5696192016-10-06 11:09:24 -0400528 {
Olli Etuahod10cf692017-11-02 11:06:14 +0200529 mDiagnostics.globalError("too many uniforms");
530 return false;
Jamie Madilld5696192016-10-06 11:09:24 -0400531 }
532 }
Olli Etuahod10cf692017-11-02 11:06:14 +0200533 if (compileOptions & SH_INIT_OUTPUT_VARIABLES)
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200534 {
Olli Etuahod10cf692017-11-02 11:06:14 +0200535 initializeOutputVariables(root);
Olli Etuahoab918822017-07-14 17:03:42 +0300536 }
alokp@chromium.org07620a52010-09-23 17:53:56 +0000537 }
538
Olli Etuahod10cf692017-11-02 11:06:14 +0200539 // gl_Position is always written in compatibility output mode.
540 // It may have been already initialized among other output variables, in that case we don't
541 // need to initialize it twice.
542 if (shaderType == GL_VERTEX_SHADER && !mGLPositionInitialized &&
543 ((compileOptions & SH_INIT_GL_POSITION) || (outputType == SH_GLSL_COMPATIBILITY_OUTPUT)))
544 {
545 initializeGLPosition(root);
546 mGLPositionInitialized = true;
547 }
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200548
Olli Etuahod10cf692017-11-02 11:06:14 +0200549 // Removing invariant declarations must be done after collecting variables.
550 // Otherwise, built-in invariant declarations don't apply.
551 if (RemoveInvariant(shaderType, shaderVersion, outputType, compileOptions))
552 {
553 sh::RemoveInvariantDeclaration(root);
554 }
555
556 if (compileOptions & SH_SCALARIZE_VEC_AND_MAT_CONSTRUCTOR_ARGS)
557 {
558 ScalarizeVecAndMatConstructorArgs(root, shaderType, fragmentPrecisionHigh, &symbolTable);
559 }
560
561 if (compileOptions & SH_REGENERATE_STRUCT_NAMES)
562 {
563 RegenerateStructNames gen(&symbolTable, shaderVersion);
564 root->traverse(&gen);
565 }
566
567 if (shaderType == GL_FRAGMENT_SHADER && shaderVersion == 100 &&
568 compileResources.EXT_draw_buffers && compileResources.MaxDrawBuffers > 1 &&
569 IsExtensionEnabled(extensionBehavior, TExtension::EXT_draw_buffers))
570 {
571 EmulateGLFragColorBroadcast(root, compileResources.MaxDrawBuffers, &outputVariables,
572 &symbolTable, shaderVersion);
573 }
574
Olli Etuahod10cf692017-11-02 11:06:14 +0200575 // Split multi declarations and remove calls to array length().
576 // Note that SimplifyLoopConditions needs to be run before any other AST transformations
577 // that may need to generate new statements from loop conditions or loop expressions.
578 SimplifyLoopConditions(
579 root,
580 IntermNodePatternMatcher::kMultiDeclaration | IntermNodePatternMatcher::kArrayLengthMethod,
581 &getSymbolTable(), getShaderVersion());
582
583 // Note that separate declarations need to be run before other AST transformations that
584 // generate new statements from expressions.
585 SeparateDeclarations(root);
586
587 SplitSequenceOperator(root, IntermNodePatternMatcher::kArrayLengthMethod, &getSymbolTable(),
588 getShaderVersion());
589
590 RemoveArrayLengthMethod(root);
591
Olli Etuahob5601eb2017-11-15 18:08:04 +0200592 // DeferGlobalInitializers needs to be run before other AST transformations that generate new
593 // statements from expressions. But it's fine to run DeferGlobalInitializers after the above
594 // SplitSequenceOperator and RemoveArrayLengthMethod since they only have an effect on the AST
595 // on ESSL >= 3.00, and the initializers that need to be deferred can only exist in ESSL < 3.00.
596 bool initializeLocalsAndGlobals =
597 (compileOptions & SH_INITIALIZE_UNINITIALIZED_LOCALS) && !IsOutputHLSL(getOutputType());
Olli Etuaho2c7f34c2017-10-09 17:18:02 +0300598 bool canUseLoopsToInitialize = !(compileOptions & SH_DONT_USE_LOOPS_TO_INITIALIZE_VARIABLES);
599 DeferGlobalInitializers(root, initializeLocalsAndGlobals, canUseLoopsToInitialize, &symbolTable);
600
Olli Etuahob5601eb2017-11-15 18:08:04 +0200601
602 if (initializeLocalsAndGlobals)
Olli Etuahod10cf692017-11-02 11:06:14 +0200603 {
604 // Initialize uninitialized local variables.
605 // In some cases initializing can generate extra statements in the parent block, such as
606 // when initializing nameless structs or initializing arrays in ESSL 1.00. In that case
607 // we need to first simplify loop conditions. We've already separated declarations
608 // earlier, which is also required. If we don't follow the Appendix A limitations, loop
609 // init statements can declare arrays or nameless structs and have multiple
610 // declarations.
611
612 if (!shouldRunLoopAndIndexingValidation(compileOptions))
613 {
614 SimplifyLoopConditions(root,
615 IntermNodePatternMatcher::kArrayDeclaration |
616 IntermNodePatternMatcher::kNamelessStructDeclaration,
617 &getSymbolTable(), getShaderVersion());
618 }
Olli Etuaho661fc482017-10-16 12:17:05 +0300619
Olli Etuaho2c7f34c2017-10-09 17:18:02 +0300620 InitializeUninitializedLocals(root, getShaderVersion(), canUseLoopsToInitialize,
621 &getSymbolTable());
Olli Etuahod10cf692017-11-02 11:06:14 +0200622 }
623
624 if (getShaderType() == GL_VERTEX_SHADER && (compileOptions & SH_CLAMP_POINT_SIZE))
625 {
626 ClampPointSize(root, compileResources.MaxPointSize, &getSymbolTable());
627 }
628
Olli Etuaho661fc482017-10-16 12:17:05 +0300629 if (compileOptions & SH_REWRITE_VECTOR_SCALAR_ARITHMETIC)
630 {
631 VectorizeVectorScalarArithmetic(root, &getSymbolTable());
632 }
633
Olli Etuahod10cf692017-11-02 11:06:14 +0200634 return true;
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200635}
636
Qiankun Miao7ebb97f2016-09-08 18:01:50 +0800637bool TCompiler::compile(const char *const shaderStrings[],
638 size_t numStrings,
639 ShCompileOptions compileOptionsIn)
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200640{
Corentin Wallez28b65282016-06-16 07:24:50 -0700641#if defined(ANGLE_ENABLE_FUZZER_CORPUS_OUTPUT)
642 DumpFuzzerCase(shaderStrings, numStrings, shaderType, shaderSpec, outputType, compileOptionsIn);
643#endif // defined(ANGLE_ENABLE_FUZZER_CORPUS_OUTPUT)
644
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200645 if (numStrings == 0)
646 return true;
647
Qiankun Miao7ebb97f2016-09-08 18:01:50 +0800648 ShCompileOptions compileOptions = compileOptionsIn;
Kenneth Russellbccc65d2016-07-19 16:48:43 -0700649
650 // Apply key workarounds.
651 if (shouldFlattenPragmaStdglInvariantAll())
652 {
653 // This should be harmless to do in all cases, but for the moment, do it only conditionally.
654 compileOptions |= SH_FLATTEN_PRAGMA_STDGL_INVARIANT_ALL;
655 }
656
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200657 TScopedPoolAllocator scopedAlloc(&allocator);
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100658 TIntermBlock *root = compileTreeImpl(shaderStrings, numStrings, compileOptions);
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200659
660 if (root)
661 {
662 if (compileOptions & SH_INTERMEDIATE_TREE)
Olli Etuahocccf2b02017-07-05 14:50:54 +0300663 OutputTree(root, infoSink.info);
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200664
665 if (compileOptions & SH_OBJECT_CODE)
Olli Etuaho89a69a02017-10-23 12:20:45 +0300666 {
667 PerformanceDiagnostics perfDiagnostics(&mDiagnostics);
668 translate(root, compileOptions, &perfDiagnostics);
669 }
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200670
671 // The IntermNode tree doesn't need to be deleted here, since the
672 // memory will be freed in a big chunk by the PoolAllocator.
673 return true;
674 }
675 return false;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000676}
677
Nicolas Capens49a88872013-06-20 09:54:03 -0400678bool TCompiler::InitBuiltInSymbolTable(const ShBuiltInResources &resources)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000679{
Olli Etuaho28cb0362016-11-22 15:42:37 +0000680 if (resources.MaxDrawBuffers < 1)
681 {
682 return false;
683 }
684 if (resources.EXT_blend_func_extended && resources.MaxDualSourceDrawBuffers < 1)
685 {
686 return false;
687 }
688
shannon.woods%transgaming.com@gtempaccount.com18b4c4b2013-04-13 03:31:40 +0000689 compileResources = resources;
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400690 setResourceString();
shannonwoods@chromium.org2ac0be92013-05-30 00:02:27 +0000691
Olli Etuaho977ee7e2017-07-21 11:38:27 +0300692 ASSERT(symbolTable.isEmpty());
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500693 symbolTable.push(); // COMMON_BUILTINS
694 symbolTable.push(); // ESSL1_BUILTINS
695 symbolTable.push(); // ESSL3_BUILTINS
696 symbolTable.push(); // ESSL3_1_BUILTINS
Olli Etuaho977ee7e2017-07-21 11:38:27 +0300697 symbolTable.push(); // GLSL_BUILTINS
shannonwoods@chromium.org2ac0be92013-05-30 00:02:27 +0000698
Jamie Madillacb4b812016-11-07 13:50:29 -0500699 switch (shaderType)
Nicolas Capens49a88872013-06-20 09:54:03 -0400700 {
Jamie Madillacb4b812016-11-07 13:50:29 -0500701 case GL_FRAGMENT_SHADER:
Olli Etuahocce89652017-06-19 16:04:09 +0300702 symbolTable.setDefaultPrecision(EbtInt, EbpMedium);
Jamie Madillacb4b812016-11-07 13:50:29 -0500703 break;
704 case GL_VERTEX_SHADER:
Jamie Madillacb4b812016-11-07 13:50:29 -0500705 case GL_COMPUTE_SHADER:
Shaob5cc1192017-07-06 10:47:20 +0800706 case GL_GEOMETRY_SHADER_OES:
Olli Etuahocce89652017-06-19 16:04:09 +0300707 symbolTable.setDefaultPrecision(EbtInt, EbpHigh);
708 symbolTable.setDefaultPrecision(EbtFloat, EbpHigh);
Jamie Madillacb4b812016-11-07 13:50:29 -0500709 break;
710 default:
Shaob5cc1192017-07-06 10:47:20 +0800711 UNREACHABLE();
Nicolas Capens49a88872013-06-20 09:54:03 -0400712 }
Olli Etuaho183d7e22015-11-20 15:59:09 +0200713 // Set defaults for sampler types that have default precision, even those that are
Zhenyao Moa5a1dfc2013-09-23 14:57:03 -0400714 // only available if an extension exists.
Olli Etuaho183d7e22015-11-20 15:59:09 +0200715 // New sampler types in ESSL3 don't have default precision. ESSL1 types do.
716 initSamplerDefaultPrecision(EbtSampler2D);
717 initSamplerDefaultPrecision(EbtSamplerCube);
718 // SamplerExternalOES is specified in the extension to have default precision.
719 initSamplerDefaultPrecision(EbtSamplerExternalOES);
Andrei Volykhina5527072017-03-22 16:46:30 +0300720 // SamplerExternal2DY2YEXT is specified in the extension to have default precision.
721 initSamplerDefaultPrecision(EbtSamplerExternal2DY2YEXT);
Olli Etuaho183d7e22015-11-20 15:59:09 +0200722 // It isn't specified whether Sampler2DRect has default precision.
723 initSamplerDefaultPrecision(EbtSampler2DRect);
Nicolas Capens49a88872013-06-20 09:54:03 -0400724
Olli Etuahocce89652017-06-19 16:04:09 +0300725 symbolTable.setDefaultPrecision(EbtAtomicCounter, EbpHigh);
jchen104cdac9e2017-05-08 11:01:20 +0800726
Jamie Madill1b452142013-07-12 14:51:11 -0400727 InsertBuiltInFunctions(shaderType, shaderSpec, resources, symbolTable);
Nicolas Capens49a88872013-06-20 09:54:03 -0400728
729 IdentifyBuiltIns(shaderType, shaderSpec, resources, symbolTable);
730
731 return true;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000732}
733
Olli Etuaho183d7e22015-11-20 15:59:09 +0200734void TCompiler::initSamplerDefaultPrecision(TBasicType samplerType)
735{
736 ASSERT(samplerType > EbtGuardSamplerBegin && samplerType < EbtGuardSamplerEnd);
Olli Etuahocce89652017-06-19 16:04:09 +0300737 symbolTable.setDefaultPrecision(samplerType, EbpLow);
Olli Etuaho183d7e22015-11-20 15:59:09 +0200738}
739
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400740void TCompiler::setResourceString()
741{
742 std::ostringstream strstream;
Geoff Langb66a9092016-05-16 15:59:14 -0400743
744 // clang-format off
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400745 strstream << ":MaxVertexAttribs:" << compileResources.MaxVertexAttribs
Jamie Madillacb4b812016-11-07 13:50:29 -0500746 << ":MaxVertexUniformVectors:" << compileResources.MaxVertexUniformVectors
747 << ":MaxVaryingVectors:" << compileResources.MaxVaryingVectors
748 << ":MaxVertexTextureImageUnits:" << compileResources.MaxVertexTextureImageUnits
749 << ":MaxCombinedTextureImageUnits:" << compileResources.MaxCombinedTextureImageUnits
750 << ":MaxTextureImageUnits:" << compileResources.MaxTextureImageUnits
751 << ":MaxFragmentUniformVectors:" << compileResources.MaxFragmentUniformVectors
752 << ":MaxDrawBuffers:" << compileResources.MaxDrawBuffers
753 << ":OES_standard_derivatives:" << compileResources.OES_standard_derivatives
754 << ":OES_EGL_image_external:" << compileResources.OES_EGL_image_external
755 << ":OES_EGL_image_external_essl3:" << compileResources.OES_EGL_image_external_essl3
756 << ":NV_EGL_stream_consumer_external:" << compileResources.NV_EGL_stream_consumer_external
757 << ":ARB_texture_rectangle:" << compileResources.ARB_texture_rectangle
758 << ":EXT_draw_buffers:" << compileResources.EXT_draw_buffers
759 << ":FragmentPrecisionHigh:" << compileResources.FragmentPrecisionHigh
760 << ":MaxExpressionComplexity:" << compileResources.MaxExpressionComplexity
761 << ":MaxCallStackDepth:" << compileResources.MaxCallStackDepth
762 << ":MaxFunctionParameters:" << compileResources.MaxFunctionParameters
763 << ":EXT_blend_func_extended:" << compileResources.EXT_blend_func_extended
764 << ":EXT_frag_depth:" << compileResources.EXT_frag_depth
765 << ":EXT_shader_texture_lod:" << compileResources.EXT_shader_texture_lod
766 << ":EXT_shader_framebuffer_fetch:" << compileResources.EXT_shader_framebuffer_fetch
767 << ":NV_shader_framebuffer_fetch:" << compileResources.NV_shader_framebuffer_fetch
768 << ":ARM_shader_framebuffer_fetch:" << compileResources.ARM_shader_framebuffer_fetch
Martin Radev318f9aa2017-05-17 17:47:28 +0300769 << ":OVR_multiview:" << compileResources.OVR_multiview
Andrei Volykhina5527072017-03-22 16:46:30 +0300770 << ":EXT_YUV_target:" << compileResources.EXT_YUV_target
Shaob5cc1192017-07-06 10:47:20 +0800771 << ":OES_geometry_shader:" << compileResources.OES_geometry_shader
Jamie Madillacb4b812016-11-07 13:50:29 -0500772 << ":MaxVertexOutputVectors:" << compileResources.MaxVertexOutputVectors
773 << ":MaxFragmentInputVectors:" << compileResources.MaxFragmentInputVectors
774 << ":MinProgramTexelOffset:" << compileResources.MinProgramTexelOffset
775 << ":MaxProgramTexelOffset:" << compileResources.MaxProgramTexelOffset
776 << ":MaxDualSourceDrawBuffers:" << compileResources.MaxDualSourceDrawBuffers
Martin Radev318f9aa2017-05-17 17:47:28 +0300777 << ":MaxViewsOVR:" << compileResources.MaxViewsOVR
Jamie Madillacb4b812016-11-07 13:50:29 -0500778 << ":NV_draw_buffers:" << compileResources.NV_draw_buffers
779 << ":WEBGL_debug_shader_precision:" << compileResources.WEBGL_debug_shader_precision
Martin Radev84aa2dc2017-09-11 15:51:02 +0300780 << ":MinProgramTextureGatherOffset:" << compileResources.MinProgramTextureGatherOffset
781 << ":MaxProgramTextureGatherOffset:" << compileResources.MaxProgramTextureGatherOffset
Jamie Madillacb4b812016-11-07 13:50:29 -0500782 << ":MaxImageUnits:" << compileResources.MaxImageUnits
783 << ":MaxVertexImageUniforms:" << compileResources.MaxVertexImageUniforms
784 << ":MaxFragmentImageUniforms:" << compileResources.MaxFragmentImageUniforms
785 << ":MaxComputeImageUniforms:" << compileResources.MaxComputeImageUniforms
786 << ":MaxCombinedImageUniforms:" << compileResources.MaxCombinedImageUniforms
787 << ":MaxCombinedShaderOutputResources:" << compileResources.MaxCombinedShaderOutputResources
788 << ":MaxComputeWorkGroupCountX:" << compileResources.MaxComputeWorkGroupCount[0]
789 << ":MaxComputeWorkGroupCountY:" << compileResources.MaxComputeWorkGroupCount[1]
790 << ":MaxComputeWorkGroupCountZ:" << compileResources.MaxComputeWorkGroupCount[2]
791 << ":MaxComputeWorkGroupSizeX:" << compileResources.MaxComputeWorkGroupSize[0]
792 << ":MaxComputeWorkGroupSizeY:" << compileResources.MaxComputeWorkGroupSize[1]
793 << ":MaxComputeWorkGroupSizeZ:" << compileResources.MaxComputeWorkGroupSize[2]
794 << ":MaxComputeUniformComponents:" << compileResources.MaxComputeUniformComponents
795 << ":MaxComputeTextureImageUnits:" << compileResources.MaxComputeTextureImageUnits
796 << ":MaxComputeAtomicCounters:" << compileResources.MaxComputeAtomicCounters
797 << ":MaxComputeAtomicCounterBuffers:" << compileResources.MaxComputeAtomicCounterBuffers
798 << ":MaxVertexAtomicCounters:" << compileResources.MaxVertexAtomicCounters
799 << ":MaxFragmentAtomicCounters:" << compileResources.MaxFragmentAtomicCounters
800 << ":MaxCombinedAtomicCounters:" << compileResources.MaxCombinedAtomicCounters
801 << ":MaxAtomicCounterBindings:" << compileResources.MaxAtomicCounterBindings
802 << ":MaxVertexAtomicCounterBuffers:" << compileResources.MaxVertexAtomicCounterBuffers
803 << ":MaxFragmentAtomicCounterBuffers:" << compileResources.MaxFragmentAtomicCounterBuffers
804 << ":MaxCombinedAtomicCounterBuffers:" << compileResources.MaxCombinedAtomicCounterBuffers
Shaob5cc1192017-07-06 10:47:20 +0800805 << ":MaxAtomicCounterBufferSize:" << compileResources.MaxAtomicCounterBufferSize
Shaob5cc1192017-07-06 10:47:20 +0800806 << ":MaxGeometryUniformComponents:" << compileResources.MaxGeometryUniformComponents
Jiawei Shaod27f5c82017-08-23 09:38:08 +0800807 << ":MaxGeometryUniformBlocks:" << compileResources.MaxGeometryUniformBlocks
808 << ":MaxGeometryInputComponents:" << compileResources.MaxGeometryInputComponents
809 << ":MaxGeometryOutputComponents:" << compileResources.MaxGeometryOutputComponents
810 << ":MaxGeometryOutputVertices:" << compileResources.MaxGeometryOutputVertices
811 << ":MaxGeometryTotalOutputComponents:" << compileResources.MaxGeometryTotalOutputComponents
812 << ":MaxGeometryTextureImageUnits:" << compileResources.MaxGeometryTextureImageUnits
813 << ":MaxGeometryAtomicCounterBuffers:" << compileResources.MaxGeometryAtomicCounterBuffers
814 << ":MaxGeometryAtomicCounters:" << compileResources.MaxGeometryAtomicCounters
815 << ":MaxGeometryShaderStorageBlocks:" << compileResources.MaxGeometryShaderStorageBlocks
816 << ":MaxGeometryShaderInvocations:" << compileResources.MaxGeometryShaderInvocations
817 << ":MaxGeometryImageUniforms:" << compileResources.MaxGeometryImageUniforms;
Geoff Langb66a9092016-05-16 15:59:14 -0400818 // clang-format on
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400819
820 builtInResourcesString = strstream.str();
821}
822
Jiajia Qin9b11ea42017-07-11 16:50:08 +0800823void TCompiler::collectInterfaceBlocks()
824{
825 ASSERT(interfaceBlocks.empty());
Jiawei Shaod8105a02017-08-08 09:54:36 +0800826 interfaceBlocks.reserve(uniformBlocks.size() + shaderStorageBlocks.size() + inBlocks.size());
Jiajia Qin9b11ea42017-07-11 16:50:08 +0800827 interfaceBlocks.insert(interfaceBlocks.end(), uniformBlocks.begin(), uniformBlocks.end());
828 interfaceBlocks.insert(interfaceBlocks.end(), shaderStorageBlocks.begin(),
829 shaderStorageBlocks.end());
Jiawei Shaod8105a02017-08-08 09:54:36 +0800830 interfaceBlocks.insert(interfaceBlocks.end(), inBlocks.begin(), inBlocks.end());
Jiajia Qin9b11ea42017-07-11 16:50:08 +0800831}
832
alokp@chromium.org07620a52010-09-23 17:53:56 +0000833void TCompiler::clearResults()
834{
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000835 arrayBoundsClamper.Cleanup();
alokp@chromium.org07620a52010-09-23 17:53:56 +0000836 infoSink.info.erase();
837 infoSink.obj.erase();
838 infoSink.debug.erase();
Olli Etuaho77ba4082016-12-16 12:01:18 +0000839 mDiagnostics.resetErrorCount();
alokp@chromium.org07620a52010-09-23 17:53:56 +0000840
Jamie Madilled27c722014-07-02 15:31:23 -0400841 attributes.clear();
842 outputVariables.clear();
alokp@chromium.org07620a52010-09-23 17:53:56 +0000843 uniforms.clear();
Jiawei-Shaodf7d7c92017-07-31 09:34:04 +0800844 inputVaryings.clear();
845 outputVaryings.clear();
Jamie Madilled27c722014-07-02 15:31:23 -0400846 interfaceBlocks.clear();
Jiajia Qin9b11ea42017-07-11 16:50:08 +0800847 uniformBlocks.clear();
848 shaderStorageBlocks.clear();
Jiawei Shaod8105a02017-08-08 09:54:36 +0800849 inBlocks.clear();
Kenneth Russellbccc65d2016-07-19 16:48:43 -0700850 variablesCollected = false;
Olli Etuahob12040c2017-06-27 14:20:45 +0300851 mGLPositionInitialized = false;
zmo@google.coma3b4ab42011-09-16 00:53:26 +0000852
Olli Etuaho09b04a22016-12-15 13:30:26 +0000853 mNumViews = -1;
854
Shaob5cc1192017-07-06 10:47:20 +0800855 mGeometryShaderInputPrimitiveType = EptUndefined;
856 mGeometryShaderOutputPrimitiveType = EptUndefined;
857 mGeometryShaderInvocations = 0;
858 mGeometryShaderMaxVertices = -1;
859
Olli Etuahodfa75e82017-01-23 09:43:06 -0800860 builtInFunctionEmulator.cleanup();
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +0000861
862 nameMap.clear();
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200863
Yunchao Hed7297bf2017-04-19 15:27:10 +0800864 mSourcePath = nullptr;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000865}
866
Corentin Wallez71d147f2015-02-11 11:15:24 -0800867bool TCompiler::initCallDag(TIntermNode *root)
zmo@google.comb1762df2011-07-30 02:04:23 +0000868{
Corentin Wallez71d147f2015-02-11 11:15:24 -0800869 mCallDag.clear();
870
Olli Etuaho77ba4082016-12-16 12:01:18 +0000871 switch (mCallDag.init(root, &mDiagnostics))
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800872 {
Jamie Madillacb4b812016-11-07 13:50:29 -0500873 case CallDAG::INITDAG_SUCCESS:
874 return true;
875 case CallDAG::INITDAG_RECURSION:
Jamie Madillacb4b812016-11-07 13:50:29 -0500876 case CallDAG::INITDAG_UNDEFINED:
Olli Etuaho77ba4082016-12-16 12:01:18 +0000877 // Error message has already been written out.
878 ASSERT(mDiagnostics.numErrors() > 0);
Jamie Madillacb4b812016-11-07 13:50:29 -0500879 return false;
Corentin Wallez71d147f2015-02-11 11:15:24 -0800880 }
881
882 UNREACHABLE();
883 return true;
884}
885
886bool TCompiler::checkCallDepth()
887{
888 std::vector<int> depths(mCallDag.size());
889
890 for (size_t i = 0; i < mCallDag.size(); i++)
891 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500892 int depth = 0;
Corentin Wallez71d147f2015-02-11 11:15:24 -0800893 auto &record = mCallDag.getRecordFromIndex(i);
894
895 for (auto &calleeIndex : record.callees)
896 {
897 depth = std::max(depth, depths[calleeIndex] + 1);
898 }
899
900 depths[i] = depth;
901
902 if (depth >= maxCallStackDepth)
903 {
904 // Trace back the function chain to have a meaningful info log.
Olli Etuaho77ba4082016-12-16 12:01:18 +0000905 std::stringstream errorStream;
906 errorStream << "Call stack too deep (larger than " << maxCallStackDepth
907 << ") with the following call chain: " << record.name;
Corentin Wallez71d147f2015-02-11 11:15:24 -0800908
Cooper Partin4d61f7e2015-08-12 10:56:50 -0700909 int currentFunction = static_cast<int>(i);
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500910 int currentDepth = depth;
Corentin Wallez71d147f2015-02-11 11:15:24 -0800911
912 while (currentFunction != -1)
913 {
Olli Etuaho77ba4082016-12-16 12:01:18 +0000914 errorStream << " -> " << mCallDag.getRecordFromIndex(currentFunction).name;
Corentin Wallez71d147f2015-02-11 11:15:24 -0800915
916 int nextFunction = -1;
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500917 for (auto &calleeIndex : mCallDag.getRecordFromIndex(currentFunction).callees)
Corentin Wallez71d147f2015-02-11 11:15:24 -0800918 {
919 if (depths[calleeIndex] == currentDepth - 1)
920 {
921 currentDepth--;
922 nextFunction = calleeIndex;
923 }
924 }
925
926 currentFunction = nextFunction;
927 }
928
Olli Etuaho77ba4082016-12-16 12:01:18 +0000929 std::string errorStr = errorStream.str();
930 mDiagnostics.globalError(errorStr.c_str());
931
Corentin Wallez71d147f2015-02-11 11:15:24 -0800932 return false;
933 }
934 }
935
936 return true;
937}
938
939bool TCompiler::tagUsedFunctions()
940{
941 // Search from main, starting from the end of the DAG as it usually is the root.
Cooper Partin4d61f7e2015-08-12 10:56:50 -0700942 for (size_t i = mCallDag.size(); i-- > 0;)
Corentin Wallez71d147f2015-02-11 11:15:24 -0800943 {
Olli Etuahoec9232b2017-03-27 17:01:37 +0300944 if (mCallDag.getRecordFromIndex(i).name == "main")
Corentin Wallez71d147f2015-02-11 11:15:24 -0800945 {
946 internalTagUsedFunction(i);
947 return true;
948 }
949 }
950
Olli Etuaho77ba4082016-12-16 12:01:18 +0000951 mDiagnostics.globalError("Missing main()");
Corentin Wallez71d147f2015-02-11 11:15:24 -0800952 return false;
953}
954
955void TCompiler::internalTagUsedFunction(size_t index)
956{
957 if (functionMetadata[index].used)
958 {
959 return;
960 }
961
962 functionMetadata[index].used = true;
963
964 for (int calleeIndex : mCallDag.getRecordFromIndex(index).callees)
965 {
966 internalTagUsedFunction(calleeIndex);
zmo@google.comb1762df2011-07-30 02:04:23 +0000967 }
968}
969
Corentin Walleza094a8a2015-04-07 11:53:06 -0700970// A predicate for the stl that returns if a top-level node is unused
971class TCompiler::UnusedPredicate
972{
973 public:
974 UnusedPredicate(const CallDAG *callDag, const std::vector<FunctionMetadata> *metadatas)
Jamie Madillacb4b812016-11-07 13:50:29 -0500975 : mCallDag(callDag), mMetadatas(metadatas)
Corentin Walleza094a8a2015-04-07 11:53:06 -0700976 {
977 }
978
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500979 bool operator()(TIntermNode *node)
Corentin Walleza094a8a2015-04-07 11:53:06 -0700980 {
Olli Etuaho16c745a2017-01-16 17:02:27 +0000981 const TIntermFunctionPrototype *asFunctionPrototype = node->getAsFunctionPrototypeNode();
982 const TIntermFunctionDefinition *asFunctionDefinition = node->getAsFunctionDefinition();
Corentin Walleza094a8a2015-04-07 11:53:06 -0700983
Olli Etuaho336b1472016-10-05 16:37:55 +0100984 const TFunctionSymbolInfo *functionInfo = nullptr;
985
Olli Etuaho16c745a2017-01-16 17:02:27 +0000986 if (asFunctionDefinition)
Olli Etuaho336b1472016-10-05 16:37:55 +0100987 {
Olli Etuaho16c745a2017-01-16 17:02:27 +0000988 functionInfo = asFunctionDefinition->getFunctionSymbolInfo();
Olli Etuaho336b1472016-10-05 16:37:55 +0100989 }
Olli Etuaho16c745a2017-01-16 17:02:27 +0000990 else if (asFunctionPrototype)
Olli Etuaho336b1472016-10-05 16:37:55 +0100991 {
Olli Etuaho16c745a2017-01-16 17:02:27 +0000992 functionInfo = asFunctionPrototype->getFunctionSymbolInfo();
Olli Etuaho336b1472016-10-05 16:37:55 +0100993 }
994 if (functionInfo == nullptr)
Corentin Walleza094a8a2015-04-07 11:53:06 -0700995 {
996 return false;
997 }
998
Olli Etuaho336b1472016-10-05 16:37:55 +0100999 size_t callDagIndex = mCallDag->findIndex(functionInfo);
Corentin Walleza094a8a2015-04-07 11:53:06 -07001000 if (callDagIndex == CallDAG::InvalidIndex)
1001 {
1002 // This happens only for unimplemented prototypes which are thus unused
Olli Etuaho16c745a2017-01-16 17:02:27 +00001003 ASSERT(asFunctionPrototype);
Corentin Walleza094a8a2015-04-07 11:53:06 -07001004 return true;
1005 }
1006
1007 ASSERT(callDagIndex < mMetadatas->size());
1008 return !(*mMetadatas)[callDagIndex].used;
1009 }
1010
1011 private:
1012 const CallDAG *mCallDag;
1013 const std::vector<FunctionMetadata> *mMetadatas;
1014};
1015
Olli Etuahod10cf692017-11-02 11:06:14 +02001016void TCompiler::pruneUnusedFunctions(TIntermBlock *root)
Corentin Walleza094a8a2015-04-07 11:53:06 -07001017{
Corentin Walleza094a8a2015-04-07 11:53:06 -07001018 UnusedPredicate isUnused(&mCallDag, &functionMetadata);
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01001019 TIntermSequence *sequence = root->getSequence();
Corentin Wallezb081e782015-07-20 05:40:04 -07001020
1021 if (!sequence->empty())
1022 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001023 sequence->erase(std::remove_if(sequence->begin(), sequence->end(), isUnused),
1024 sequence->end());
Corentin Wallezb081e782015-07-20 05:40:04 -07001025 }
Corentin Walleza094a8a2015-04-07 11:53:06 -07001026}
1027
Olli Etuahob4279202017-05-18 15:08:24 +03001028bool TCompiler::limitExpressionComplexity(TIntermBlock *root)
Jamie Madilleb1a0102013-07-08 13:31:38 -04001029{
Olli Etuahocccf2b02017-07-05 14:50:54 +03001030 if (!IsASTDepthBelowLimit(root, maxExpressionComplexity))
Jamie Madill6654bc92014-03-26 14:01:57 -04001031 {
Olli Etuaho77ba4082016-12-16 12:01:18 +00001032 mDiagnostics.globalError("Expression too complex.");
Jamie Madill6654bc92014-03-26 14:01:57 -04001033 return false;
1034 }
1035
Olli Etuahob4279202017-05-18 15:08:24 +03001036 if (!ValidateMaxParameters(root, maxFunctionParameters))
Olli Etuaho19d1dc92016-03-08 17:18:46 +02001037 {
Olli Etuaho77ba4082016-12-16 12:01:18 +00001038 mDiagnostics.globalError("Function has too many parameters.");
Olli Etuaho19d1dc92016-03-08 17:18:46 +02001039 return false;
1040 }
1041
Jamie Madilleb1a0102013-07-08 13:31:38 -04001042 return true;
1043}
1044
Corentin Wallez1df16022016-10-27 08:16:56 -04001045bool TCompiler::shouldCollectVariables(ShCompileOptions compileOptions)
1046{
1047 return (compileOptions & SH_VARIABLES) != 0;
1048}
1049
1050bool TCompiler::wereVariablesCollected() const
1051{
1052 return variablesCollected;
1053}
1054
Olli Etuaho9cbc07c2017-05-10 18:22:01 +03001055void TCompiler::initializeGLPosition(TIntermBlock *root)
Zhenyao Mo4a667fe2014-02-11 12:35:01 -08001056{
Zhenyao Mo72111912016-07-20 17:45:56 -07001057 InitVariableList list;
Olli Etuahoe7c28572017-10-23 16:29:33 +03001058 sh::ShaderVariable var(GL_FLOAT_VEC4);
Zhenyao Mo72111912016-07-20 17:45:56 -07001059 var.name = "gl_Position";
1060 list.push_back(var);
Olli Etuaho2c7f34c2017-10-09 17:18:02 +03001061 InitializeVariables(root, list, &symbolTable, shaderVersion, extensionBehavior, false);
Zhenyao Mo4a667fe2014-02-11 12:35:01 -08001062}
1063
Olli Etuaho9cbc07c2017-05-10 18:22:01 +03001064void TCompiler::useAllMembersInUnusedStandardAndSharedBlocks(TIntermBlock *root)
Qin Jiajia7835b522016-10-08 11:20:17 +08001065{
1066 sh::InterfaceBlockList list;
1067
Jiajia Qin9b11ea42017-07-11 16:50:08 +08001068 for (auto block : uniformBlocks)
Qin Jiajia7835b522016-10-08 11:20:17 +08001069 {
1070 if (!block.staticUse &&
Qin Jiajiaca68d982017-09-18 16:41:56 +08001071 (block.layout == sh::BLOCKLAYOUT_STD140 || block.layout == sh::BLOCKLAYOUT_SHARED))
Qin Jiajia7835b522016-10-08 11:20:17 +08001072 {
1073 list.push_back(block);
1074 }
1075 }
1076
Zhenyao Mod7490962016-11-09 15:49:51 -08001077 sh::UseInterfaceBlockFields(root, list, symbolTable);
Qin Jiajia7835b522016-10-08 11:20:17 +08001078}
1079
Olli Etuaho9cbc07c2017-05-10 18:22:01 +03001080void TCompiler::initializeOutputVariables(TIntermBlock *root)
Zhenyao Mo4a667fe2014-02-11 12:35:01 -08001081{
Zhenyao Mo72111912016-07-20 17:45:56 -07001082 InitVariableList list;
Jiawei Shaod27f5c82017-08-23 09:38:08 +08001083 if (shaderType == GL_VERTEX_SHADER || shaderType == GL_GEOMETRY_SHADER_OES)
Zhenyao Mo4a667fe2014-02-11 12:35:01 -08001084 {
Jiawei-Shaodf7d7c92017-07-31 09:34:04 +08001085 for (auto var : outputVaryings)
Zhenyao Mo4a667fe2014-02-11 12:35:01 -08001086 {
Zhenyao Mof9312682016-07-22 12:51:31 -07001087 list.push_back(var);
Olli Etuahob12040c2017-06-27 14:20:45 +03001088 if (var.name == "gl_Position")
1089 {
1090 ASSERT(!mGLPositionInitialized);
1091 mGLPositionInitialized = true;
1092 }
Zhenyao Mo4a667fe2014-02-11 12:35:01 -08001093 }
Zhenyao Mo4a667fe2014-02-11 12:35:01 -08001094 }
Zhenyao Mo72111912016-07-20 17:45:56 -07001095 else
1096 {
1097 ASSERT(shaderType == GL_FRAGMENT_SHADER);
1098 for (auto var : outputVariables)
1099 {
1100 list.push_back(var);
1101 }
1102 }
Olli Etuaho2c7f34c2017-10-09 17:18:02 +03001103 InitializeVariables(root, list, &symbolTable, shaderVersion, extensionBehavior, false);
Zhenyao Mo4a667fe2014-02-11 12:35:01 -08001104}
1105
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001106const TExtensionBehavior &TCompiler::getExtensionBehavior() const
zmo@google.com5601ea02011-06-10 18:23:25 +00001107{
1108 return extensionBehavior;
1109}
zmo@google.com32e97312011-08-24 01:03:11 +00001110
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02001111const char *TCompiler::getSourcePath() const
1112{
1113 return mSourcePath;
1114}
1115
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001116const ShBuiltInResources &TCompiler::getResources() const
shannon.woods%transgaming.com@gtempaccount.com18b4c4b2013-04-13 03:31:40 +00001117{
1118 return compileResources;
1119}
1120
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001121const ArrayBoundsClamper &TCompiler::getArrayBoundsClamper() const
daniel@transgaming.com4167cc92013-01-11 04:11:53 +00001122{
1123 return arrayBoundsClamper;
1124}
1125
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +00001126ShArrayIndexClampingStrategy TCompiler::getArrayIndexClampingStrategy() const
1127{
1128 return clampingStrategy;
1129}
1130
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001131const BuiltInFunctionEmulator &TCompiler::getBuiltInFunctionEmulator() const
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +00001132{
1133 return builtInFunctionEmulator;
1134}
Zhenyao Mo94ac7b72014-10-15 18:22:08 -07001135
Qiankun Miao7ebb97f2016-09-08 18:01:50 +08001136void TCompiler::writePragma(ShCompileOptions compileOptions)
Zhenyao Mo94ac7b72014-10-15 18:22:08 -07001137{
Kenneth Russellbccc65d2016-07-19 16:48:43 -07001138 if (!(compileOptions & SH_FLATTEN_PRAGMA_STDGL_INVARIANT_ALL))
1139 {
1140 TInfoSinkBase &sink = infoSink.obj;
1141 if (mPragma.stdgl.invariantAll)
1142 sink << "#pragma STDGL invariant(all)\n";
1143 }
1144}
1145
1146bool TCompiler::isVaryingDefined(const char *varyingName)
1147{
1148 ASSERT(variablesCollected);
Jiawei-Shaodf7d7c92017-07-31 09:34:04 +08001149 for (size_t ii = 0; ii < inputVaryings.size(); ++ii)
Kenneth Russellbccc65d2016-07-19 16:48:43 -07001150 {
Jiawei-Shaodf7d7c92017-07-31 09:34:04 +08001151 if (inputVaryings[ii].name == varyingName)
1152 {
1153 return true;
1154 }
1155 }
1156 for (size_t ii = 0; ii < outputVaryings.size(); ++ii)
1157 {
1158 if (outputVaryings[ii].name == varyingName)
Kenneth Russellbccc65d2016-07-19 16:48:43 -07001159 {
1160 return true;
1161 }
1162 }
1163
1164 return false;
Zhenyao Mo94ac7b72014-10-15 18:22:08 -07001165}
Jamie Madillacb4b812016-11-07 13:50:29 -05001166
1167} // namespace sh