Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 1 | // |
| 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 Etuaho | d57e0db | 2015-04-24 15:05:08 +0300 | [diff] [blame] | 8 | |
Zhenyao Mo | 7211191 | 2016-07-20 17:45:56 -0700 | [diff] [blame] | 9 | #include "angle_gl.h" |
Olli Etuaho | d57e0db | 2015-04-24 15:05:08 +0300 | [diff] [blame] | 10 | #include "common/debug.h" |
Olli Etuaho | 9cbc07c | 2017-05-10 18:22:01 +0300 | [diff] [blame] | 11 | #include "compiler/translator/FindMain.h" |
Olli Etuaho | 3ec7568 | 2017-07-05 17:02:55 +0300 | [diff] [blame] | 12 | #include "compiler/translator/IntermNode_util.h" |
Olli Etuaho | cccf2b0 | 2017-07-05 14:50:54 +0300 | [diff] [blame] | 13 | #include "compiler/translator/IntermTraverse.h" |
Olli Etuaho | b60d30f | 2018-01-16 12:31:06 +0200 | [diff] [blame] | 14 | #include "compiler/translator/StaticType.h" |
Zhenyao Mo | d749096 | 2016-11-09 15:49:51 -0800 | [diff] [blame] | 15 | #include "compiler/translator/SymbolTable.h" |
Zhenyao Mo | 7211191 | 2016-07-20 17:45:56 -0700 | [diff] [blame] | 16 | #include "compiler/translator/util.h" |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 17 | |
Jamie Madill | 45bcc78 | 2016-11-07 13:58:48 -0500 | [diff] [blame] | 18 | namespace sh |
| 19 | { |
| 20 | |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 21 | namespace |
| 22 | { |
| 23 | |
Olli Etuaho | 9733cee | 2017-05-11 19:14:35 +0300 | [diff] [blame] | 24 | void AddArrayZeroInitSequence(const TIntermTyped *initializedNode, |
Olli Etuaho | 2c7f34c | 2017-10-09 17:18:02 +0300 | [diff] [blame] | 25 | bool canUseLoopsToInitialize, |
Olli Etuaho | 87cc90d | 2017-12-12 15:28:06 +0200 | [diff] [blame] | 26 | bool highPrecisionSupported, |
Olli Etuaho | 2c7f34c | 2017-10-09 17:18:02 +0300 | [diff] [blame] | 27 | TIntermSequence *initSequenceOut, |
| 28 | TSymbolTable *symbolTable); |
| 29 | |
| 30 | void AddStructZeroInitSequence(const TIntermTyped *initializedNode, |
| 31 | bool canUseLoopsToInitialize, |
Olli Etuaho | 87cc90d | 2017-12-12 15:28:06 +0200 | [diff] [blame] | 32 | bool highPrecisionSupported, |
Olli Etuaho | 2c7f34c | 2017-10-09 17:18:02 +0300 | [diff] [blame] | 33 | TIntermSequence *initSequenceOut, |
| 34 | TSymbolTable *symbolTable); |
Olli Etuaho | 9733cee | 2017-05-11 19:14:35 +0300 | [diff] [blame] | 35 | |
| 36 | TIntermBinary *CreateZeroInitAssignment(const TIntermTyped *initializedNode) |
| 37 | { |
Olli Etuaho | 3ec7568 | 2017-07-05 17:02:55 +0300 | [diff] [blame] | 38 | TIntermTyped *zero = CreateZeroNode(initializedNode->getType()); |
Olli Etuaho | 9733cee | 2017-05-11 19:14:35 +0300 | [diff] [blame] | 39 | return new TIntermBinary(EOpAssign, initializedNode->deepCopy(), zero); |
| 40 | } |
| 41 | |
Olli Etuaho | 2c7f34c | 2017-10-09 17:18:02 +0300 | [diff] [blame] | 42 | void AddZeroInitSequence(const TIntermTyped *initializedNode, |
| 43 | bool canUseLoopsToInitialize, |
Olli Etuaho | 87cc90d | 2017-12-12 15:28:06 +0200 | [diff] [blame] | 44 | bool highPrecisionSupported, |
Olli Etuaho | 2c7f34c | 2017-10-09 17:18:02 +0300 | [diff] [blame] | 45 | TIntermSequence *initSequenceOut, |
| 46 | TSymbolTable *symbolTable) |
| 47 | { |
| 48 | if (initializedNode->isArray()) |
| 49 | { |
Olli Etuaho | 87cc90d | 2017-12-12 15:28:06 +0200 | [diff] [blame] | 50 | AddArrayZeroInitSequence(initializedNode, canUseLoopsToInitialize, highPrecisionSupported, |
| 51 | initSequenceOut, symbolTable); |
Olli Etuaho | 2c7f34c | 2017-10-09 17:18:02 +0300 | [diff] [blame] | 52 | } |
| 53 | else if (initializedNode->getType().isStructureContainingArrays() || |
| 54 | initializedNode->getType().isNamelessStruct()) |
| 55 | { |
Olli Etuaho | 87cc90d | 2017-12-12 15:28:06 +0200 | [diff] [blame] | 56 | AddStructZeroInitSequence(initializedNode, canUseLoopsToInitialize, highPrecisionSupported, |
| 57 | initSequenceOut, symbolTable); |
Olli Etuaho | 2c7f34c | 2017-10-09 17:18:02 +0300 | [diff] [blame] | 58 | } |
| 59 | else |
| 60 | { |
| 61 | initSequenceOut->push_back(CreateZeroInitAssignment(initializedNode)); |
| 62 | } |
| 63 | } |
| 64 | |
Olli Etuaho | 9733cee | 2017-05-11 19:14:35 +0300 | [diff] [blame] | 65 | void AddStructZeroInitSequence(const TIntermTyped *initializedNode, |
Olli Etuaho | 2c7f34c | 2017-10-09 17:18:02 +0300 | [diff] [blame] | 66 | bool canUseLoopsToInitialize, |
Olli Etuaho | 87cc90d | 2017-12-12 15:28:06 +0200 | [diff] [blame] | 67 | bool highPrecisionSupported, |
Olli Etuaho | 2c7f34c | 2017-10-09 17:18:02 +0300 | [diff] [blame] | 68 | TIntermSequence *initSequenceOut, |
| 69 | TSymbolTable *symbolTable) |
Olli Etuaho | 9733cee | 2017-05-11 19:14:35 +0300 | [diff] [blame] | 70 | { |
| 71 | ASSERT(initializedNode->getBasicType() == EbtStruct); |
Olli Etuaho | bd3cd50 | 2017-11-03 15:48:52 +0200 | [diff] [blame] | 72 | const TStructure *structType = initializedNode->getType().getStruct(); |
Olli Etuaho | 9733cee | 2017-05-11 19:14:35 +0300 | [diff] [blame] | 73 | for (int i = 0; i < static_cast<int>(structType->fields().size()); ++i) |
| 74 | { |
Olli Etuaho | 3ec7568 | 2017-07-05 17:02:55 +0300 | [diff] [blame] | 75 | TIntermBinary *element = new TIntermBinary(EOpIndexDirectStruct, |
| 76 | initializedNode->deepCopy(), CreateIndexNode(i)); |
Olli Etuaho | 2c7f34c | 2017-10-09 17:18:02 +0300 | [diff] [blame] | 77 | // 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 Etuaho | 87cc90d | 2017-12-12 15:28:06 +0200 | [diff] [blame] | 80 | AddZeroInitSequence(element, canUseLoopsToInitialize, highPrecisionSupported, |
| 81 | initSequenceOut, symbolTable); |
Olli Etuaho | 9733cee | 2017-05-11 19:14:35 +0300 | [diff] [blame] | 82 | } |
| 83 | } |
| 84 | |
Olli Etuaho | 2c7f34c | 2017-10-09 17:18:02 +0300 | [diff] [blame] | 85 | void AddArrayZeroInitStatementList(const TIntermTyped *initializedNode, |
| 86 | bool canUseLoopsToInitialize, |
Olli Etuaho | 87cc90d | 2017-12-12 15:28:06 +0200 | [diff] [blame] | 87 | bool highPrecisionSupported, |
Olli Etuaho | 2c7f34c | 2017-10-09 17:18:02 +0300 | [diff] [blame] | 88 | TIntermSequence *initSequenceOut, |
| 89 | TSymbolTable *symbolTable) |
Olli Etuaho | 9733cee | 2017-05-11 19:14:35 +0300 | [diff] [blame] | 90 | { |
Olli Etuaho | 96f6adf | 2017-08-16 11:18:54 +0300 | [diff] [blame] | 91 | for (unsigned int i = 0; i < initializedNode->getOutermostArraySize(); ++i) |
Olli Etuaho | 9733cee | 2017-05-11 19:14:35 +0300 | [diff] [blame] | 92 | { |
Olli Etuaho | 3ec7568 | 2017-07-05 17:02:55 +0300 | [diff] [blame] | 93 | TIntermBinary *element = |
| 94 | new TIntermBinary(EOpIndexDirect, initializedNode->deepCopy(), CreateIndexNode(i)); |
Olli Etuaho | 87cc90d | 2017-12-12 15:28:06 +0200 | [diff] [blame] | 95 | AddZeroInitSequence(element, canUseLoopsToInitialize, highPrecisionSupported, |
| 96 | initSequenceOut, symbolTable); |
Olli Etuaho | 2c7f34c | 2017-10-09 17:18:02 +0300 | [diff] [blame] | 97 | } |
| 98 | } |
| 99 | |
| 100 | void AddArrayZeroInitForLoop(const TIntermTyped *initializedNode, |
Olli Etuaho | 87cc90d | 2017-12-12 15:28:06 +0200 | [diff] [blame] | 101 | bool highPrecisionSupported, |
Olli Etuaho | 2c7f34c | 2017-10-09 17:18:02 +0300 | [diff] [blame] | 102 | TIntermSequence *initSequenceOut, |
| 103 | TSymbolTable *symbolTable) |
| 104 | { |
| 105 | ASSERT(initializedNode->isArray()); |
Olli Etuaho | b60d30f | 2018-01-16 12:31:06 +0200 | [diff] [blame] | 106 | 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 Etuaho | 2c7f34c | 2017-10-09 17:18:02 +0300 | [diff] [blame] | 110 | |
Olli Etuaho | 195be94 | 2017-12-04 23:40:14 +0200 | [diff] [blame] | 111 | TIntermSymbol *indexSymbolNode = CreateTempSymbolNode(indexVariable); |
Olli Etuaho | 2c7f34c | 2017-10-09 17:18:02 +0300 | [diff] [blame] | 112 | TIntermDeclaration *indexInit = |
Olli Etuaho | 195be94 | 2017-12-04 23:40:14 +0200 | [diff] [blame] | 113 | CreateTempInitDeclarationNode(indexVariable, CreateZeroNode(indexVariable->getType())); |
Olli Etuaho | 2c7f34c | 2017-10-09 17:18:02 +0300 | [diff] [blame] | 114 | 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 Etuaho | 87cc90d | 2017-12-12 15:28:06 +0200 | [diff] [blame] | 124 | AddZeroInitSequence(element, true, highPrecisionSupported, forLoopBodySeq, symbolTable); |
Olli Etuaho | 2c7f34c | 2017-10-09 17:18:02 +0300 | [diff] [blame] | 125 | |
| 126 | TIntermLoop *forLoop = |
| 127 | new TIntermLoop(ELoopFor, indexInit, indexSmallerThanSize, indexIncrement, forLoopBody); |
| 128 | initSequenceOut->push_back(forLoop); |
| 129 | } |
| 130 | |
| 131 | void AddArrayZeroInitSequence(const TIntermTyped *initializedNode, |
| 132 | bool canUseLoopsToInitialize, |
Olli Etuaho | 87cc90d | 2017-12-12 15:28:06 +0200 | [diff] [blame] | 133 | bool highPrecisionSupported, |
Olli Etuaho | 2c7f34c | 2017-10-09 17:18:02 +0300 | [diff] [blame] | 134 | 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 Etuaho | 87cc90d | 2017-12-12 15:28:06 +0200 | [diff] [blame] | 151 | AddArrayZeroInitStatementList(initializedNode, canUseLoopsToInitialize, |
| 152 | highPrecisionSupported, initSequenceOut, symbolTable); |
Olli Etuaho | 2c7f34c | 2017-10-09 17:18:02 +0300 | [diff] [blame] | 153 | } |
| 154 | else |
| 155 | { |
Olli Etuaho | 87cc90d | 2017-12-12 15:28:06 +0200 | [diff] [blame] | 156 | AddArrayZeroInitForLoop(initializedNode, highPrecisionSupported, initSequenceOut, |
| 157 | symbolTable); |
Olli Etuaho | 9733cee | 2017-05-11 19:14:35 +0300 | [diff] [blame] | 158 | } |
| 159 | } |
| 160 | |
| 161 | void InsertInitCode(TIntermSequence *mainBody, |
Olli Etuaho | 9cbc07c | 2017-05-10 18:22:01 +0300 | [diff] [blame] | 162 | const InitVariableList &variables, |
Olli Etuaho | 2c7f34c | 2017-10-09 17:18:02 +0300 | [diff] [blame] | 163 | TSymbolTable *symbolTable, |
Martin Radev | e145def | 2017-06-22 12:49:12 +0300 | [diff] [blame] | 164 | int shaderVersion, |
Olli Etuaho | 2c7f34c | 2017-10-09 17:18:02 +0300 | [diff] [blame] | 165 | const TExtensionBehavior &extensionBehavior, |
Olli Etuaho | 87cc90d | 2017-12-12 15:28:06 +0200 | [diff] [blame] | 166 | bool canUseLoopsToInitialize, |
| 167 | bool highPrecisionSupported) |
Zhenyao Mo | 7211191 | 2016-07-20 17:45:56 -0700 | [diff] [blame] | 168 | { |
Olli Etuaho | 9cbc07c | 2017-05-10 18:22:01 +0300 | [diff] [blame] | 169 | for (const auto &var : variables) |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 170 | { |
Zhenyao Mo | 7211191 | 2016-07-20 17:45:56 -0700 | [diff] [blame] | 171 | TString name = TString(var.name.c_str()); |
Martin Radev | e145def | 2017-06-22 12:49:12 +0300 | [diff] [blame] | 172 | size_t pos = name.find_last_of('['); |
| 173 | if (pos != TString::npos) |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 174 | { |
Martin Radev | e145def | 2017-06-22 12:49:12 +0300 | [diff] [blame] | 175 | name = name.substr(0, pos); |
Zhenyao Mo | f931268 | 2016-07-22 12:51:31 -0700 | [diff] [blame] | 176 | } |
Zhenyao Mo | d749096 | 2016-11-09 15:49:51 -0800 | [diff] [blame] | 177 | |
Olli Etuaho | daaff1c | 2017-07-05 18:03:26 +0300 | [diff] [blame] | 178 | TIntermTyped *initializedSymbol = nullptr; |
Martin Radev | e145def | 2017-06-22 12:49:12 +0300 | [diff] [blame] | 179 | if (var.isBuiltIn()) |
| 180 | { |
Olli Etuaho | 2c7f34c | 2017-10-09 17:18:02 +0300 | [diff] [blame] | 181 | initializedSymbol = ReferenceBuiltInVariable(name, *symbolTable, shaderVersion); |
Olli Etuaho | daaff1c | 2017-07-05 18:03:26 +0300 | [diff] [blame] | 182 | if (initializedSymbol->getQualifier() == EvqFragData && |
Olli Etuaho | 2a1e8f9 | 2017-07-14 11:49:36 +0300 | [diff] [blame] | 183 | !IsExtensionEnabled(extensionBehavior, TExtension::EXT_draw_buffers)) |
Olli Etuaho | daaff1c | 2017-07-05 18:03:26 +0300 | [diff] [blame] | 184 | { |
| 185 | // If GL_EXT_draw_buffers is disabled, only the 0th index of gl_FragData can be |
| 186 | // written to. |
| 187 | // TODO(oetuaho): This is a bit hacky and would be better to remove, if we came up |
| 188 | // with a good way to do it. Right now "gl_FragData" in symbol table is initialized |
| 189 | // to have the array size of MaxDrawBuffers, and the initialization happens before |
| 190 | // the shader sets the extensions it is using. |
| 191 | initializedSymbol = |
| 192 | new TIntermBinary(EOpIndexDirect, initializedSymbol, CreateIndexNode(0)); |
| 193 | } |
Zhenyao Mo | d749096 | 2016-11-09 15:49:51 -0800 | [diff] [blame] | 194 | } |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 195 | else |
| 196 | { |
Olli Etuaho | 2c7f34c | 2017-10-09 17:18:02 +0300 | [diff] [blame] | 197 | initializedSymbol = ReferenceGlobalVariable(name, *symbolTable); |
Corentin Wallez | 509e456 | 2016-08-25 14:55:44 -0400 | [diff] [blame] | 198 | } |
Olli Etuaho | daaff1c | 2017-07-05 18:03:26 +0300 | [diff] [blame] | 199 | ASSERT(initializedSymbol != nullptr); |
Martin Radev | e145def | 2017-06-22 12:49:12 +0300 | [diff] [blame] | 200 | |
Olli Etuaho | 87cc90d | 2017-12-12 15:28:06 +0200 | [diff] [blame] | 201 | TIntermSequence *initCode = CreateInitCode(initializedSymbol, canUseLoopsToInitialize, |
| 202 | highPrecisionSupported, symbolTable); |
Olli Etuaho | 9733cee | 2017-05-11 19:14:35 +0300 | [diff] [blame] | 203 | mainBody->insert(mainBody->begin(), initCode->begin(), initCode->end()); |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 204 | } |
| 205 | } |
| 206 | |
Olli Etuaho | 9733cee | 2017-05-11 19:14:35 +0300 | [diff] [blame] | 207 | class InitializeLocalsTraverser : public TIntermTraverser |
| 208 | { |
| 209 | public: |
Olli Etuaho | 2c7f34c | 2017-10-09 17:18:02 +0300 | [diff] [blame] | 210 | InitializeLocalsTraverser(int shaderVersion, |
| 211 | TSymbolTable *symbolTable, |
Olli Etuaho | 87cc90d | 2017-12-12 15:28:06 +0200 | [diff] [blame] | 212 | bool canUseLoopsToInitialize, |
| 213 | bool highPrecisionSupported) |
Olli Etuaho | 2c7f34c | 2017-10-09 17:18:02 +0300 | [diff] [blame] | 214 | : TIntermTraverser(true, false, false, symbolTable), |
| 215 | mShaderVersion(shaderVersion), |
Olli Etuaho | 87cc90d | 2017-12-12 15:28:06 +0200 | [diff] [blame] | 216 | mCanUseLoopsToInitialize(canUseLoopsToInitialize), |
| 217 | mHighPrecisionSupported(highPrecisionSupported) |
Olli Etuaho | 9733cee | 2017-05-11 19:14:35 +0300 | [diff] [blame] | 218 | { |
| 219 | } |
| 220 | |
| 221 | protected: |
| 222 | bool visitDeclaration(Visit visit, TIntermDeclaration *node) override |
| 223 | { |
| 224 | for (TIntermNode *declarator : *node->getSequence()) |
| 225 | { |
| 226 | if (!mInGlobalScope && !declarator->getAsBinaryNode()) |
| 227 | { |
| 228 | TIntermSymbol *symbol = declarator->getAsSymbolNode(); |
| 229 | ASSERT(symbol); |
Olli Etuaho | 8b5e8fd | 2017-12-15 14:59:15 +0200 | [diff] [blame] | 230 | if (symbol->variable().symbolType() == SymbolType::Empty) |
Olli Etuaho | 9733cee | 2017-05-11 19:14:35 +0300 | [diff] [blame] | 231 | { |
| 232 | continue; |
| 233 | } |
| 234 | |
| 235 | // Arrays may need to be initialized one element at a time, since ESSL 1.00 does not |
| 236 | // support array constructors or assigning arrays. |
| 237 | bool arrayConstructorUnavailable = |
| 238 | (symbol->isArray() || symbol->getType().isStructureContainingArrays()) && |
| 239 | mShaderVersion == 100; |
| 240 | // Nameless struct constructors can't be referred to, so they also need to be |
| 241 | // initialized one element at a time. |
Olli Etuaho | 2c7f34c | 2017-10-09 17:18:02 +0300 | [diff] [blame] | 242 | // TODO(oetuaho): Check if it makes sense to initialize using a loop, even if we |
| 243 | // could use an initializer. It could at least reduce code size for very large |
| 244 | // arrays, but could hurt runtime performance. |
Olli Etuaho | 858ff48 | 2017-11-15 13:41:46 +0200 | [diff] [blame] | 245 | if (arrayConstructorUnavailable || symbol->getType().isNamelessStruct()) |
Olli Etuaho | 9733cee | 2017-05-11 19:14:35 +0300 | [diff] [blame] | 246 | { |
| 247 | // SimplifyLoopConditions should have been run so the parent node of this node |
| 248 | // should not be a loop. |
| 249 | ASSERT(getParentNode()->getAsLoopNode() == nullptr); |
| 250 | // SeparateDeclarations should have already been run, so we don't need to worry |
| 251 | // about further declarators in this declaration depending on the effects of |
| 252 | // this declarator. |
| 253 | ASSERT(node->getSequence()->size() == 1); |
Olli Etuaho | 2c7f34c | 2017-10-09 17:18:02 +0300 | [diff] [blame] | 254 | insertStatementsInParentBlock( |
Olli Etuaho | 87cc90d | 2017-12-12 15:28:06 +0200 | [diff] [blame] | 255 | TIntermSequence(), *CreateInitCode(symbol, mCanUseLoopsToInitialize, |
| 256 | mHighPrecisionSupported, mSymbolTable)); |
Olli Etuaho | 9733cee | 2017-05-11 19:14:35 +0300 | [diff] [blame] | 257 | } |
| 258 | else |
| 259 | { |
Olli Etuaho | 3ec7568 | 2017-07-05 17:02:55 +0300 | [diff] [blame] | 260 | TIntermBinary *init = |
| 261 | new TIntermBinary(EOpInitialize, symbol, CreateZeroNode(symbol->getType())); |
Olli Etuaho | 9733cee | 2017-05-11 19:14:35 +0300 | [diff] [blame] | 262 | queueReplacementWithParent(node, symbol, init, OriginalNode::BECOMES_CHILD); |
| 263 | } |
| 264 | } |
| 265 | } |
| 266 | return false; |
| 267 | } |
| 268 | |
| 269 | private: |
| 270 | int mShaderVersion; |
Olli Etuaho | 2c7f34c | 2017-10-09 17:18:02 +0300 | [diff] [blame] | 271 | bool mCanUseLoopsToInitialize; |
Olli Etuaho | 87cc90d | 2017-12-12 15:28:06 +0200 | [diff] [blame] | 272 | bool mHighPrecisionSupported; |
Olli Etuaho | 9733cee | 2017-05-11 19:14:35 +0300 | [diff] [blame] | 273 | }; |
| 274 | |
Zhenyao Mo | 7211191 | 2016-07-20 17:45:56 -0700 | [diff] [blame] | 275 | } // namespace anonymous |
| 276 | |
Olli Etuaho | 2c7f34c | 2017-10-09 17:18:02 +0300 | [diff] [blame] | 277 | TIntermSequence *CreateInitCode(const TIntermTyped *initializedSymbol, |
| 278 | bool canUseLoopsToInitialize, |
Olli Etuaho | 87cc90d | 2017-12-12 15:28:06 +0200 | [diff] [blame] | 279 | bool highPrecisionSupported, |
Olli Etuaho | 2c7f34c | 2017-10-09 17:18:02 +0300 | [diff] [blame] | 280 | TSymbolTable *symbolTable) |
Olli Etuaho | 9733cee | 2017-05-11 19:14:35 +0300 | [diff] [blame] | 281 | { |
| 282 | TIntermSequence *initCode = new TIntermSequence(); |
Olli Etuaho | 87cc90d | 2017-12-12 15:28:06 +0200 | [diff] [blame] | 283 | AddZeroInitSequence(initializedSymbol, canUseLoopsToInitialize, highPrecisionSupported, |
| 284 | initCode, symbolTable); |
Olli Etuaho | 9733cee | 2017-05-11 19:14:35 +0300 | [diff] [blame] | 285 | return initCode; |
| 286 | } |
| 287 | |
Olli Etuaho | 2c7f34c | 2017-10-09 17:18:02 +0300 | [diff] [blame] | 288 | void InitializeUninitializedLocals(TIntermBlock *root, |
| 289 | int shaderVersion, |
| 290 | bool canUseLoopsToInitialize, |
Olli Etuaho | 87cc90d | 2017-12-12 15:28:06 +0200 | [diff] [blame] | 291 | bool highPrecisionSupported, |
Olli Etuaho | 2c7f34c | 2017-10-09 17:18:02 +0300 | [diff] [blame] | 292 | TSymbolTable *symbolTable) |
Olli Etuaho | 9733cee | 2017-05-11 19:14:35 +0300 | [diff] [blame] | 293 | { |
Olli Etuaho | 87cc90d | 2017-12-12 15:28:06 +0200 | [diff] [blame] | 294 | InitializeLocalsTraverser traverser(shaderVersion, symbolTable, canUseLoopsToInitialize, |
| 295 | highPrecisionSupported); |
Olli Etuaho | 9733cee | 2017-05-11 19:14:35 +0300 | [diff] [blame] | 296 | root->traverse(&traverser); |
| 297 | traverser.updateTree(); |
| 298 | } |
| 299 | |
Olli Etuaho | 9cbc07c | 2017-05-10 18:22:01 +0300 | [diff] [blame] | 300 | void InitializeVariables(TIntermBlock *root, |
Zhenyao Mo | d749096 | 2016-11-09 15:49:51 -0800 | [diff] [blame] | 301 | const InitVariableList &vars, |
Olli Etuaho | 2c7f34c | 2017-10-09 17:18:02 +0300 | [diff] [blame] | 302 | TSymbolTable *symbolTable, |
Martin Radev | e145def | 2017-06-22 12:49:12 +0300 | [diff] [blame] | 303 | int shaderVersion, |
Olli Etuaho | 2c7f34c | 2017-10-09 17:18:02 +0300 | [diff] [blame] | 304 | const TExtensionBehavior &extensionBehavior, |
Olli Etuaho | 87cc90d | 2017-12-12 15:28:06 +0200 | [diff] [blame] | 305 | bool canUseLoopsToInitialize, |
| 306 | bool highPrecisionSupported) |
Zhenyao Mo | 7211191 | 2016-07-20 17:45:56 -0700 | [diff] [blame] | 307 | { |
Martin Radev | b50ccd3 | 2017-07-06 17:09:58 +0300 | [diff] [blame] | 308 | TIntermBlock *body = FindMainBody(root); |
Olli Etuaho | 2c7f34c | 2017-10-09 17:18:02 +0300 | [diff] [blame] | 309 | InsertInitCode(body->getSequence(), vars, symbolTable, shaderVersion, extensionBehavior, |
Olli Etuaho | 87cc90d | 2017-12-12 15:28:06 +0200 | [diff] [blame] | 310 | canUseLoopsToInitialize, highPrecisionSupported); |
Zhenyao Mo | 7211191 | 2016-07-20 17:45:56 -0700 | [diff] [blame] | 311 | } |
Jamie Madill | 45bcc78 | 2016-11-07 13:58:48 -0500 | [diff] [blame] | 312 | |
| 313 | } // namespace sh |