blob: 6914f02a2bf94aec045c51d340fd501df6e9240d [file] [log] [blame]
Zhenyao Mo4a667fe2014-02-11 12:35:01 -08001//
2// Copyright (c) 2002-2013 The ANGLE Project Authors. All rights reserved.
3// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
7#include "compiler/translator/InitializeVariables.h"
Olli Etuahod57e0db2015-04-24 15:05:08 +03008
Zhenyao Mo72111912016-07-20 17:45:56 -07009#include "angle_gl.h"
Olli Etuahod57e0db2015-04-24 15:05:08 +030010#include "common/debug.h"
Olli Etuahob60d30f2018-01-16 12:31:06 +020011#include "compiler/translator/StaticType.h"
Zhenyao Mod7490962016-11-09 15:49:51 -080012#include "compiler/translator/SymbolTable.h"
Olli Etuahoc26214d2018-03-16 10:43:11 +020013#include "compiler/translator/tree_util/FindMain.h"
14#include "compiler/translator/tree_util/IntermNode_util.h"
15#include "compiler/translator/tree_util/IntermTraverse.h"
Zhenyao Mo72111912016-07-20 17:45:56 -070016#include "compiler/translator/util.h"
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080017
Jamie Madill45bcc782016-11-07 13:58:48 -050018namespace sh
19{
20
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080021namespace
22{
23
Olli Etuaho9733cee2017-05-11 19:14:35 +030024void AddArrayZeroInitSequence(const TIntermTyped *initializedNode,
Olli Etuaho2c7f34c2017-10-09 17:18:02 +030025 bool canUseLoopsToInitialize,
Olli Etuaho87cc90d2017-12-12 15:28:06 +020026 bool highPrecisionSupported,
Olli Etuaho2c7f34c2017-10-09 17:18:02 +030027 TIntermSequence *initSequenceOut,
28 TSymbolTable *symbolTable);
29
30void AddStructZeroInitSequence(const TIntermTyped *initializedNode,
31 bool canUseLoopsToInitialize,
Olli Etuaho87cc90d2017-12-12 15:28:06 +020032 bool highPrecisionSupported,
Olli Etuaho2c7f34c2017-10-09 17:18:02 +030033 TIntermSequence *initSequenceOut,
34 TSymbolTable *symbolTable);
Olli Etuaho9733cee2017-05-11 19:14:35 +030035
36TIntermBinary *CreateZeroInitAssignment(const TIntermTyped *initializedNode)
37{
Olli Etuaho3ec75682017-07-05 17:02:55 +030038 TIntermTyped *zero = CreateZeroNode(initializedNode->getType());
Olli Etuaho9733cee2017-05-11 19:14:35 +030039 return new TIntermBinary(EOpAssign, initializedNode->deepCopy(), zero);
40}
41
Olli Etuaho2c7f34c2017-10-09 17:18:02 +030042void AddZeroInitSequence(const TIntermTyped *initializedNode,
43 bool canUseLoopsToInitialize,
Olli Etuaho87cc90d2017-12-12 15:28:06 +020044 bool highPrecisionSupported,
Olli Etuaho2c7f34c2017-10-09 17:18:02 +030045 TIntermSequence *initSequenceOut,
46 TSymbolTable *symbolTable)
47{
48 if (initializedNode->isArray())
49 {
Olli Etuaho87cc90d2017-12-12 15:28:06 +020050 AddArrayZeroInitSequence(initializedNode, canUseLoopsToInitialize, highPrecisionSupported,
51 initSequenceOut, symbolTable);
Olli Etuaho2c7f34c2017-10-09 17:18:02 +030052 }
53 else if (initializedNode->getType().isStructureContainingArrays() ||
54 initializedNode->getType().isNamelessStruct())
55 {
Olli Etuaho87cc90d2017-12-12 15:28:06 +020056 AddStructZeroInitSequence(initializedNode, canUseLoopsToInitialize, highPrecisionSupported,
57 initSequenceOut, symbolTable);
Olli Etuaho2c7f34c2017-10-09 17:18:02 +030058 }
59 else
60 {
61 initSequenceOut->push_back(CreateZeroInitAssignment(initializedNode));
62 }
63}
64
Olli Etuaho9733cee2017-05-11 19:14:35 +030065void AddStructZeroInitSequence(const TIntermTyped *initializedNode,
Olli Etuaho2c7f34c2017-10-09 17:18:02 +030066 bool canUseLoopsToInitialize,
Olli Etuaho87cc90d2017-12-12 15:28:06 +020067 bool highPrecisionSupported,
Olli Etuaho2c7f34c2017-10-09 17:18:02 +030068 TIntermSequence *initSequenceOut,
69 TSymbolTable *symbolTable)
Olli Etuaho9733cee2017-05-11 19:14:35 +030070{
71 ASSERT(initializedNode->getBasicType() == EbtStruct);
Olli Etuahobd3cd502017-11-03 15:48:52 +020072 const TStructure *structType = initializedNode->getType().getStruct();
Olli Etuaho9733cee2017-05-11 19:14:35 +030073 for (int i = 0; i < static_cast<int>(structType->fields().size()); ++i)
74 {
Olli Etuaho3ec75682017-07-05 17:02:55 +030075 TIntermBinary *element = new TIntermBinary(EOpIndexDirectStruct,
76 initializedNode->deepCopy(), CreateIndexNode(i));
Olli Etuaho2c7f34c2017-10-09 17:18:02 +030077 // Structs can't be defined inside structs, so the type of a struct field can't be a
78 // nameless struct.
79 ASSERT(!element->getType().isNamelessStruct());
Olli Etuaho87cc90d2017-12-12 15:28:06 +020080 AddZeroInitSequence(element, canUseLoopsToInitialize, highPrecisionSupported,
81 initSequenceOut, symbolTable);
Olli Etuaho9733cee2017-05-11 19:14:35 +030082 }
83}
84
Olli Etuaho2c7f34c2017-10-09 17:18:02 +030085void AddArrayZeroInitStatementList(const TIntermTyped *initializedNode,
86 bool canUseLoopsToInitialize,
Olli Etuaho87cc90d2017-12-12 15:28:06 +020087 bool highPrecisionSupported,
Olli Etuaho2c7f34c2017-10-09 17:18:02 +030088 TIntermSequence *initSequenceOut,
89 TSymbolTable *symbolTable)
Olli Etuaho9733cee2017-05-11 19:14:35 +030090{
Olli Etuaho96f6adf2017-08-16 11:18:54 +030091 for (unsigned int i = 0; i < initializedNode->getOutermostArraySize(); ++i)
Olli Etuaho9733cee2017-05-11 19:14:35 +030092 {
Olli Etuaho3ec75682017-07-05 17:02:55 +030093 TIntermBinary *element =
94 new TIntermBinary(EOpIndexDirect, initializedNode->deepCopy(), CreateIndexNode(i));
Olli Etuaho87cc90d2017-12-12 15:28:06 +020095 AddZeroInitSequence(element, canUseLoopsToInitialize, highPrecisionSupported,
96 initSequenceOut, symbolTable);
Olli Etuaho2c7f34c2017-10-09 17:18:02 +030097 }
98}
99
100void AddArrayZeroInitForLoop(const TIntermTyped *initializedNode,
Olli Etuaho87cc90d2017-12-12 15:28:06 +0200101 bool highPrecisionSupported,
Olli Etuaho2c7f34c2017-10-09 17:18:02 +0300102 TIntermSequence *initSequenceOut,
103 TSymbolTable *symbolTable)
104{
105 ASSERT(initializedNode->isArray());
Olli Etuahob60d30f2018-01-16 12:31:06 +0200106 const TType *mediumpIndexType = StaticType::Get<EbtInt, EbpMedium, EvqTemporary, 1, 1>();
107 const TType *highpIndexType = StaticType::Get<EbtInt, EbpHigh, EvqTemporary, 1, 1>();
108 TVariable *indexVariable =
109 CreateTempVariable(symbolTable, highPrecisionSupported ? highpIndexType : mediumpIndexType);
Olli Etuaho2c7f34c2017-10-09 17:18:02 +0300110
Olli Etuaho195be942017-12-04 23:40:14 +0200111 TIntermSymbol *indexSymbolNode = CreateTempSymbolNode(indexVariable);
Olli Etuaho2c7f34c2017-10-09 17:18:02 +0300112 TIntermDeclaration *indexInit =
Olli Etuaho195be942017-12-04 23:40:14 +0200113 CreateTempInitDeclarationNode(indexVariable, CreateZeroNode(indexVariable->getType()));
Olli Etuaho2c7f34c2017-10-09 17:18:02 +0300114 TIntermConstantUnion *arraySizeNode = CreateIndexNode(initializedNode->getOutermostArraySize());
115 TIntermBinary *indexSmallerThanSize =
116 new TIntermBinary(EOpLessThan, indexSymbolNode->deepCopy(), arraySizeNode);
117 TIntermUnary *indexIncrement = new TIntermUnary(EOpPreIncrement, indexSymbolNode->deepCopy());
118
119 TIntermBlock *forLoopBody = new TIntermBlock();
120 TIntermSequence *forLoopBodySeq = forLoopBody->getSequence();
121
122 TIntermBinary *element = new TIntermBinary(EOpIndexIndirect, initializedNode->deepCopy(),
123 indexSymbolNode->deepCopy());
Olli Etuaho87cc90d2017-12-12 15:28:06 +0200124 AddZeroInitSequence(element, true, highPrecisionSupported, forLoopBodySeq, symbolTable);
Olli Etuaho2c7f34c2017-10-09 17:18:02 +0300125
126 TIntermLoop *forLoop =
127 new TIntermLoop(ELoopFor, indexInit, indexSmallerThanSize, indexIncrement, forLoopBody);
128 initSequenceOut->push_back(forLoop);
129}
130
131void AddArrayZeroInitSequence(const TIntermTyped *initializedNode,
132 bool canUseLoopsToInitialize,
Olli Etuaho87cc90d2017-12-12 15:28:06 +0200133 bool highPrecisionSupported,
Olli Etuaho2c7f34c2017-10-09 17:18:02 +0300134 TIntermSequence *initSequenceOut,
135 TSymbolTable *symbolTable)
136{
137 // The array elements are assigned one by one to keep the AST compatible with ESSL 1.00 which
138 // doesn't have array assignment. We'll do this either with a for loop or just a list of
139 // statements assigning to each array index. Note that it is important to have the array init in
140 // the right order to workaround http://crbug.com/709317
141 bool isSmallArray = initializedNode->getOutermostArraySize() <= 1u ||
142 (initializedNode->getBasicType() != EbtStruct &&
143 !initializedNode->getType().isArrayOfArrays() &&
144 initializedNode->getOutermostArraySize() <= 3u);
145 if (initializedNode->getQualifier() == EvqFragData ||
146 initializedNode->getQualifier() == EvqFragmentOut || isSmallArray ||
147 !canUseLoopsToInitialize)
148 {
149 // Fragment outputs should not be indexed by non-constant indices.
150 // Also it doesn't make sense to use loops to initialize very small arrays.
Olli Etuaho87cc90d2017-12-12 15:28:06 +0200151 AddArrayZeroInitStatementList(initializedNode, canUseLoopsToInitialize,
152 highPrecisionSupported, initSequenceOut, symbolTable);
Olli Etuaho2c7f34c2017-10-09 17:18:02 +0300153 }
154 else
155 {
Olli Etuaho87cc90d2017-12-12 15:28:06 +0200156 AddArrayZeroInitForLoop(initializedNode, highPrecisionSupported, initSequenceOut,
157 symbolTable);
Olli Etuaho9733cee2017-05-11 19:14:35 +0300158 }
159}
160
161void InsertInitCode(TIntermSequence *mainBody,
Olli Etuaho9cbc07c2017-05-10 18:22:01 +0300162 const InitVariableList &variables,
Olli Etuaho2c7f34c2017-10-09 17:18:02 +0300163 TSymbolTable *symbolTable,
Martin Radeve145def2017-06-22 12:49:12 +0300164 int shaderVersion,
Olli Etuaho2c7f34c2017-10-09 17:18:02 +0300165 const TExtensionBehavior &extensionBehavior,
Olli Etuaho87cc90d2017-12-12 15:28:06 +0200166 bool canUseLoopsToInitialize,
167 bool highPrecisionSupported)
Zhenyao Mo72111912016-07-20 17:45:56 -0700168{
Olli Etuaho9cbc07c2017-05-10 18:22:01 +0300169 for (const auto &var : variables)
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800170 {
Olli Etuahobf107be2018-02-08 18:43:03 +0200171 // Note that tempVariableName will reference a short-lived char array here - that's fine
172 // since we're only using it to find symbols.
173 ImmutableString tempVariableName(var.name.c_str(), var.name.length());
Zhenyao Mod7490962016-11-09 15:49:51 -0800174
Olli Etuahodaaff1c2017-07-05 18:03:26 +0300175 TIntermTyped *initializedSymbol = nullptr;
Martin Radeve145def2017-06-22 12:49:12 +0300176 if (var.isBuiltIn())
177 {
Olli Etuahobf107be2018-02-08 18:43:03 +0200178 initializedSymbol =
179 ReferenceBuiltInVariable(tempVariableName, *symbolTable, shaderVersion);
Olli Etuahodaaff1c2017-07-05 18:03:26 +0300180 if (initializedSymbol->getQualifier() == EvqFragData &&
Olli Etuaho2a1e8f92017-07-14 11:49:36 +0300181 !IsExtensionEnabled(extensionBehavior, TExtension::EXT_draw_buffers))
Olli Etuahodaaff1c2017-07-05 18:03:26 +0300182 {
183 // If GL_EXT_draw_buffers is disabled, only the 0th index of gl_FragData can be
184 // written to.
185 // TODO(oetuaho): This is a bit hacky and would be better to remove, if we came up
186 // with a good way to do it. Right now "gl_FragData" in symbol table is initialized
187 // to have the array size of MaxDrawBuffers, and the initialization happens before
188 // the shader sets the extensions it is using.
189 initializedSymbol =
190 new TIntermBinary(EOpIndexDirect, initializedSymbol, CreateIndexNode(0));
191 }
Zhenyao Mod7490962016-11-09 15:49:51 -0800192 }
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800193 else
194 {
Olli Etuahobf107be2018-02-08 18:43:03 +0200195 initializedSymbol = ReferenceGlobalVariable(tempVariableName, *symbolTable);
Corentin Wallez509e4562016-08-25 14:55:44 -0400196 }
Olli Etuahodaaff1c2017-07-05 18:03:26 +0300197 ASSERT(initializedSymbol != nullptr);
Martin Radeve145def2017-06-22 12:49:12 +0300198
Olli Etuaho87cc90d2017-12-12 15:28:06 +0200199 TIntermSequence *initCode = CreateInitCode(initializedSymbol, canUseLoopsToInitialize,
200 highPrecisionSupported, symbolTable);
Olli Etuaho9733cee2017-05-11 19:14:35 +0300201 mainBody->insert(mainBody->begin(), initCode->begin(), initCode->end());
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800202 }
203}
204
Olli Etuaho9733cee2017-05-11 19:14:35 +0300205class InitializeLocalsTraverser : public TIntermTraverser
206{
207 public:
Olli Etuaho2c7f34c2017-10-09 17:18:02 +0300208 InitializeLocalsTraverser(int shaderVersion,
209 TSymbolTable *symbolTable,
Olli Etuaho87cc90d2017-12-12 15:28:06 +0200210 bool canUseLoopsToInitialize,
211 bool highPrecisionSupported)
Olli Etuaho2c7f34c2017-10-09 17:18:02 +0300212 : TIntermTraverser(true, false, false, symbolTable),
213 mShaderVersion(shaderVersion),
Olli Etuaho87cc90d2017-12-12 15:28:06 +0200214 mCanUseLoopsToInitialize(canUseLoopsToInitialize),
215 mHighPrecisionSupported(highPrecisionSupported)
Olli Etuaho9733cee2017-05-11 19:14:35 +0300216 {
217 }
218
219 protected:
220 bool visitDeclaration(Visit visit, TIntermDeclaration *node) override
221 {
222 for (TIntermNode *declarator : *node->getSequence())
223 {
224 if (!mInGlobalScope && !declarator->getAsBinaryNode())
225 {
226 TIntermSymbol *symbol = declarator->getAsSymbolNode();
227 ASSERT(symbol);
Olli Etuaho8b5e8fd2017-12-15 14:59:15 +0200228 if (symbol->variable().symbolType() == SymbolType::Empty)
Olli Etuaho9733cee2017-05-11 19:14:35 +0300229 {
230 continue;
231 }
232
233 // Arrays may need to be initialized one element at a time, since ESSL 1.00 does not
234 // support array constructors or assigning arrays.
235 bool arrayConstructorUnavailable =
236 (symbol->isArray() || symbol->getType().isStructureContainingArrays()) &&
237 mShaderVersion == 100;
238 // Nameless struct constructors can't be referred to, so they also need to be
239 // initialized one element at a time.
Olli Etuaho2c7f34c2017-10-09 17:18:02 +0300240 // TODO(oetuaho): Check if it makes sense to initialize using a loop, even if we
241 // could use an initializer. It could at least reduce code size for very large
242 // arrays, but could hurt runtime performance.
Olli Etuaho858ff482017-11-15 13:41:46 +0200243 if (arrayConstructorUnavailable || symbol->getType().isNamelessStruct())
Olli Etuaho9733cee2017-05-11 19:14:35 +0300244 {
245 // SimplifyLoopConditions should have been run so the parent node of this node
246 // should not be a loop.
247 ASSERT(getParentNode()->getAsLoopNode() == nullptr);
248 // SeparateDeclarations should have already been run, so we don't need to worry
249 // about further declarators in this declaration depending on the effects of
250 // this declarator.
251 ASSERT(node->getSequence()->size() == 1);
Olli Etuaho2c7f34c2017-10-09 17:18:02 +0300252 insertStatementsInParentBlock(
Olli Etuaho87cc90d2017-12-12 15:28:06 +0200253 TIntermSequence(), *CreateInitCode(symbol, mCanUseLoopsToInitialize,
254 mHighPrecisionSupported, mSymbolTable));
Olli Etuaho9733cee2017-05-11 19:14:35 +0300255 }
256 else
257 {
Olli Etuaho3ec75682017-07-05 17:02:55 +0300258 TIntermBinary *init =
259 new TIntermBinary(EOpInitialize, symbol, CreateZeroNode(symbol->getType()));
Olli Etuaho9733cee2017-05-11 19:14:35 +0300260 queueReplacementWithParent(node, symbol, init, OriginalNode::BECOMES_CHILD);
261 }
262 }
263 }
264 return false;
265 }
266
267 private:
268 int mShaderVersion;
Olli Etuaho2c7f34c2017-10-09 17:18:02 +0300269 bool mCanUseLoopsToInitialize;
Olli Etuaho87cc90d2017-12-12 15:28:06 +0200270 bool mHighPrecisionSupported;
Olli Etuaho9733cee2017-05-11 19:14:35 +0300271};
272
Zhenyao Mo72111912016-07-20 17:45:56 -0700273} // namespace anonymous
274
Olli Etuaho2c7f34c2017-10-09 17:18:02 +0300275TIntermSequence *CreateInitCode(const TIntermTyped *initializedSymbol,
276 bool canUseLoopsToInitialize,
Olli Etuaho87cc90d2017-12-12 15:28:06 +0200277 bool highPrecisionSupported,
Olli Etuaho2c7f34c2017-10-09 17:18:02 +0300278 TSymbolTable *symbolTable)
Olli Etuaho9733cee2017-05-11 19:14:35 +0300279{
280 TIntermSequence *initCode = new TIntermSequence();
Olli Etuaho87cc90d2017-12-12 15:28:06 +0200281 AddZeroInitSequence(initializedSymbol, canUseLoopsToInitialize, highPrecisionSupported,
282 initCode, symbolTable);
Olli Etuaho9733cee2017-05-11 19:14:35 +0300283 return initCode;
284}
285
Olli Etuaho2c7f34c2017-10-09 17:18:02 +0300286void InitializeUninitializedLocals(TIntermBlock *root,
287 int shaderVersion,
288 bool canUseLoopsToInitialize,
Olli Etuaho87cc90d2017-12-12 15:28:06 +0200289 bool highPrecisionSupported,
Olli Etuaho2c7f34c2017-10-09 17:18:02 +0300290 TSymbolTable *symbolTable)
Olli Etuaho9733cee2017-05-11 19:14:35 +0300291{
Olli Etuaho87cc90d2017-12-12 15:28:06 +0200292 InitializeLocalsTraverser traverser(shaderVersion, symbolTable, canUseLoopsToInitialize,
293 highPrecisionSupported);
Olli Etuaho9733cee2017-05-11 19:14:35 +0300294 root->traverse(&traverser);
295 traverser.updateTree();
296}
297
Olli Etuaho9cbc07c2017-05-10 18:22:01 +0300298void InitializeVariables(TIntermBlock *root,
Zhenyao Mod7490962016-11-09 15:49:51 -0800299 const InitVariableList &vars,
Olli Etuaho2c7f34c2017-10-09 17:18:02 +0300300 TSymbolTable *symbolTable,
Martin Radeve145def2017-06-22 12:49:12 +0300301 int shaderVersion,
Olli Etuaho2c7f34c2017-10-09 17:18:02 +0300302 const TExtensionBehavior &extensionBehavior,
Olli Etuaho87cc90d2017-12-12 15:28:06 +0200303 bool canUseLoopsToInitialize,
304 bool highPrecisionSupported)
Zhenyao Mo72111912016-07-20 17:45:56 -0700305{
Martin Radevb50ccd32017-07-06 17:09:58 +0300306 TIntermBlock *body = FindMainBody(root);
Olli Etuaho2c7f34c2017-10-09 17:18:02 +0300307 InsertInitCode(body->getSequence(), vars, symbolTable, shaderVersion, extensionBehavior,
Olli Etuaho87cc90d2017-12-12 15:28:06 +0200308 canUseLoopsToInitialize, highPrecisionSupported);
Zhenyao Mo72111912016-07-20 17:45:56 -0700309}
Jamie Madill45bcc782016-11-07 13:58:48 -0500310
311} // namespace sh