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" |
Zhenyao Mo | d749096 | 2016-11-09 15:49:51 -0800 | [diff] [blame] | 14 | #include "compiler/translator/SymbolTable.h" |
Zhenyao Mo | 7211191 | 2016-07-20 17:45:56 -0700 | [diff] [blame] | 15 | #include "compiler/translator/util.h" |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 16 | |
Jamie Madill | 45bcc78 | 2016-11-07 13:58:48 -0500 | [diff] [blame] | 17 | namespace sh |
| 18 | { |
| 19 | |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 20 | namespace |
| 21 | { |
| 22 | |
Olli Etuaho | 9733cee | 2017-05-11 19:14:35 +0300 | [diff] [blame] | 23 | bool IsNamelessStruct(const TIntermTyped *node) |
| 24 | { |
| 25 | return (node->getBasicType() == EbtStruct && node->getType().getStruct()->name() == ""); |
| 26 | } |
| 27 | |
| 28 | void AddArrayZeroInitSequence(const TIntermTyped *initializedNode, |
| 29 | TIntermSequence *initSequenceOut); |
| 30 | |
| 31 | TIntermBinary *CreateZeroInitAssignment(const TIntermTyped *initializedNode) |
| 32 | { |
Olli Etuaho | 3ec7568 | 2017-07-05 17:02:55 +0300 | [diff] [blame] | 33 | TIntermTyped *zero = CreateZeroNode(initializedNode->getType()); |
Olli Etuaho | 9733cee | 2017-05-11 19:14:35 +0300 | [diff] [blame] | 34 | return new TIntermBinary(EOpAssign, initializedNode->deepCopy(), zero); |
| 35 | } |
| 36 | |
| 37 | void AddStructZeroInitSequence(const TIntermTyped *initializedNode, |
| 38 | TIntermSequence *initSequenceOut) |
| 39 | { |
| 40 | ASSERT(initializedNode->getBasicType() == EbtStruct); |
| 41 | TStructure *structType = initializedNode->getType().getStruct(); |
| 42 | for (int i = 0; i < static_cast<int>(structType->fields().size()); ++i) |
| 43 | { |
Olli Etuaho | 3ec7568 | 2017-07-05 17:02:55 +0300 | [diff] [blame] | 44 | TIntermBinary *element = new TIntermBinary(EOpIndexDirectStruct, |
| 45 | initializedNode->deepCopy(), CreateIndexNode(i)); |
Olli Etuaho | 9733cee | 2017-05-11 19:14:35 +0300 | [diff] [blame] | 46 | if (element->isArray()) |
| 47 | { |
| 48 | AddArrayZeroInitSequence(element, initSequenceOut); |
| 49 | } |
| 50 | else if (element->getType().isStructureContainingArrays()) |
| 51 | { |
| 52 | AddStructZeroInitSequence(element, initSequenceOut); |
| 53 | } |
| 54 | else |
| 55 | { |
| 56 | // Structs can't be defined inside structs, so the type of a struct field can't be a |
| 57 | // nameless struct. |
| 58 | ASSERT(!IsNamelessStruct(element)); |
| 59 | initSequenceOut->push_back(CreateZeroInitAssignment(element)); |
| 60 | } |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | void AddArrayZeroInitSequence(const TIntermTyped *initializedNode, TIntermSequence *initSequenceOut) |
| 65 | { |
| 66 | ASSERT(initializedNode->isArray()); |
| 67 | // Assign the array elements one by one to keep the AST compatible with ESSL 1.00 which |
| 68 | // doesn't have array assignment. |
| 69 | // Note that it is important to have the array init in the right order to workaround |
| 70 | // http://crbug.com/709317 |
Olli Etuaho | 96f6adf | 2017-08-16 11:18:54 +0300 | [diff] [blame] | 71 | for (unsigned int i = 0; i < initializedNode->getOutermostArraySize(); ++i) |
Olli Etuaho | 9733cee | 2017-05-11 19:14:35 +0300 | [diff] [blame] | 72 | { |
Olli Etuaho | 3ec7568 | 2017-07-05 17:02:55 +0300 | [diff] [blame] | 73 | TIntermBinary *element = |
| 74 | new TIntermBinary(EOpIndexDirect, initializedNode->deepCopy(), CreateIndexNode(i)); |
Olli Etuaho | 96f6adf | 2017-08-16 11:18:54 +0300 | [diff] [blame] | 75 | if (element->isArray()) |
| 76 | { |
| 77 | AddArrayZeroInitSequence(element, initSequenceOut); |
| 78 | } |
| 79 | else if (element->getType().isStructureContainingArrays()) |
Olli Etuaho | 9733cee | 2017-05-11 19:14:35 +0300 | [diff] [blame] | 80 | { |
| 81 | AddStructZeroInitSequence(element, initSequenceOut); |
| 82 | } |
| 83 | else |
| 84 | { |
| 85 | initSequenceOut->push_back(CreateZeroInitAssignment(element)); |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | void InsertInitCode(TIntermSequence *mainBody, |
Olli Etuaho | 9cbc07c | 2017-05-10 18:22:01 +0300 | [diff] [blame] | 91 | const InitVariableList &variables, |
Martin Radev | e145def | 2017-06-22 12:49:12 +0300 | [diff] [blame] | 92 | const TSymbolTable &symbolTable, |
| 93 | int shaderVersion, |
Martin Radev | e145def | 2017-06-22 12:49:12 +0300 | [diff] [blame] | 94 | const TExtensionBehavior &extensionBehavior) |
Zhenyao Mo | 7211191 | 2016-07-20 17:45:56 -0700 | [diff] [blame] | 95 | { |
Olli Etuaho | 9cbc07c | 2017-05-10 18:22:01 +0300 | [diff] [blame] | 96 | for (const auto &var : variables) |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 97 | { |
Zhenyao Mo | 7211191 | 2016-07-20 17:45:56 -0700 | [diff] [blame] | 98 | TString name = TString(var.name.c_str()); |
Martin Radev | e145def | 2017-06-22 12:49:12 +0300 | [diff] [blame] | 99 | size_t pos = name.find_last_of('['); |
| 100 | if (pos != TString::npos) |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 101 | { |
Martin Radev | e145def | 2017-06-22 12:49:12 +0300 | [diff] [blame] | 102 | name = name.substr(0, pos); |
Zhenyao Mo | f931268 | 2016-07-22 12:51:31 -0700 | [diff] [blame] | 103 | } |
Zhenyao Mo | d749096 | 2016-11-09 15:49:51 -0800 | [diff] [blame] | 104 | |
Olli Etuaho | daaff1c | 2017-07-05 18:03:26 +0300 | [diff] [blame] | 105 | TIntermTyped *initializedSymbol = nullptr; |
Martin Radev | e145def | 2017-06-22 12:49:12 +0300 | [diff] [blame] | 106 | if (var.isBuiltIn()) |
| 107 | { |
Olli Etuaho | daaff1c | 2017-07-05 18:03:26 +0300 | [diff] [blame] | 108 | initializedSymbol = ReferenceBuiltInVariable(name, symbolTable, shaderVersion); |
| 109 | if (initializedSymbol->getQualifier() == EvqFragData && |
| 110 | !IsExtensionEnabled(extensionBehavior, "GL_EXT_draw_buffers")) |
| 111 | { |
| 112 | // If GL_EXT_draw_buffers is disabled, only the 0th index of gl_FragData can be |
| 113 | // written to. |
| 114 | // TODO(oetuaho): This is a bit hacky and would be better to remove, if we came up |
| 115 | // with a good way to do it. Right now "gl_FragData" in symbol table is initialized |
| 116 | // to have the array size of MaxDrawBuffers, and the initialization happens before |
| 117 | // the shader sets the extensions it is using. |
| 118 | initializedSymbol = |
| 119 | new TIntermBinary(EOpIndexDirect, initializedSymbol, CreateIndexNode(0)); |
| 120 | } |
Zhenyao Mo | d749096 | 2016-11-09 15:49:51 -0800 | [diff] [blame] | 121 | } |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 122 | else |
| 123 | { |
Olli Etuaho | daaff1c | 2017-07-05 18:03:26 +0300 | [diff] [blame] | 124 | initializedSymbol = ReferenceGlobalVariable(name, symbolTable); |
Corentin Wallez | 509e456 | 2016-08-25 14:55:44 -0400 | [diff] [blame] | 125 | } |
Olli Etuaho | daaff1c | 2017-07-05 18:03:26 +0300 | [diff] [blame] | 126 | ASSERT(initializedSymbol != nullptr); |
Martin Radev | e145def | 2017-06-22 12:49:12 +0300 | [diff] [blame] | 127 | |
Olli Etuaho | 9733cee | 2017-05-11 19:14:35 +0300 | [diff] [blame] | 128 | TIntermSequence *initCode = CreateInitCode(initializedSymbol); |
| 129 | mainBody->insert(mainBody->begin(), initCode->begin(), initCode->end()); |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 130 | } |
| 131 | } |
| 132 | |
Olli Etuaho | 9733cee | 2017-05-11 19:14:35 +0300 | [diff] [blame] | 133 | class InitializeLocalsTraverser : public TIntermTraverser |
| 134 | { |
| 135 | public: |
| 136 | InitializeLocalsTraverser(int shaderVersion) |
| 137 | : TIntermTraverser(true, false, false), mShaderVersion(shaderVersion) |
| 138 | { |
| 139 | } |
| 140 | |
| 141 | protected: |
| 142 | bool visitDeclaration(Visit visit, TIntermDeclaration *node) override |
| 143 | { |
| 144 | for (TIntermNode *declarator : *node->getSequence()) |
| 145 | { |
| 146 | if (!mInGlobalScope && !declarator->getAsBinaryNode()) |
| 147 | { |
| 148 | TIntermSymbol *symbol = declarator->getAsSymbolNode(); |
| 149 | ASSERT(symbol); |
| 150 | if (symbol->getSymbol() == "") |
| 151 | { |
| 152 | continue; |
| 153 | } |
| 154 | |
| 155 | // Arrays may need to be initialized one element at a time, since ESSL 1.00 does not |
| 156 | // support array constructors or assigning arrays. |
| 157 | bool arrayConstructorUnavailable = |
| 158 | (symbol->isArray() || symbol->getType().isStructureContainingArrays()) && |
| 159 | mShaderVersion == 100; |
| 160 | // Nameless struct constructors can't be referred to, so they also need to be |
| 161 | // initialized one element at a time. |
| 162 | if (arrayConstructorUnavailable || IsNamelessStruct(symbol)) |
| 163 | { |
| 164 | // SimplifyLoopConditions should have been run so the parent node of this node |
| 165 | // should not be a loop. |
| 166 | ASSERT(getParentNode()->getAsLoopNode() == nullptr); |
| 167 | // SeparateDeclarations should have already been run, so we don't need to worry |
| 168 | // about further declarators in this declaration depending on the effects of |
| 169 | // this declarator. |
| 170 | ASSERT(node->getSequence()->size() == 1); |
| 171 | insertStatementsInParentBlock(TIntermSequence(), *CreateInitCode(symbol)); |
| 172 | } |
| 173 | else |
| 174 | { |
Olli Etuaho | 3ec7568 | 2017-07-05 17:02:55 +0300 | [diff] [blame] | 175 | TIntermBinary *init = |
| 176 | new TIntermBinary(EOpInitialize, symbol, CreateZeroNode(symbol->getType())); |
Olli Etuaho | 9733cee | 2017-05-11 19:14:35 +0300 | [diff] [blame] | 177 | queueReplacementWithParent(node, symbol, init, OriginalNode::BECOMES_CHILD); |
| 178 | } |
| 179 | } |
| 180 | } |
| 181 | return false; |
| 182 | } |
| 183 | |
| 184 | private: |
| 185 | int mShaderVersion; |
| 186 | }; |
| 187 | |
Zhenyao Mo | 7211191 | 2016-07-20 17:45:56 -0700 | [diff] [blame] | 188 | } // namespace anonymous |
| 189 | |
Olli Etuaho | daaff1c | 2017-07-05 18:03:26 +0300 | [diff] [blame] | 190 | TIntermSequence *CreateInitCode(const TIntermTyped *initializedSymbol) |
Olli Etuaho | 9733cee | 2017-05-11 19:14:35 +0300 | [diff] [blame] | 191 | { |
| 192 | TIntermSequence *initCode = new TIntermSequence(); |
| 193 | if (initializedSymbol->isArray()) |
| 194 | { |
| 195 | AddArrayZeroInitSequence(initializedSymbol, initCode); |
| 196 | } |
| 197 | else if (initializedSymbol->getType().isStructureContainingArrays() || |
| 198 | IsNamelessStruct(initializedSymbol)) |
| 199 | { |
| 200 | AddStructZeroInitSequence(initializedSymbol, initCode); |
| 201 | } |
| 202 | else |
| 203 | { |
| 204 | initCode->push_back(CreateZeroInitAssignment(initializedSymbol)); |
| 205 | } |
| 206 | return initCode; |
| 207 | } |
| 208 | |
| 209 | void InitializeUninitializedLocals(TIntermBlock *root, int shaderVersion) |
| 210 | { |
| 211 | InitializeLocalsTraverser traverser(shaderVersion); |
| 212 | root->traverse(&traverser); |
| 213 | traverser.updateTree(); |
| 214 | } |
| 215 | |
Olli Etuaho | 9cbc07c | 2017-05-10 18:22:01 +0300 | [diff] [blame] | 216 | void InitializeVariables(TIntermBlock *root, |
Zhenyao Mo | d749096 | 2016-11-09 15:49:51 -0800 | [diff] [blame] | 217 | const InitVariableList &vars, |
Martin Radev | e145def | 2017-06-22 12:49:12 +0300 | [diff] [blame] | 218 | const TSymbolTable &symbolTable, |
| 219 | int shaderVersion, |
Martin Radev | e145def | 2017-06-22 12:49:12 +0300 | [diff] [blame] | 220 | const TExtensionBehavior &extensionBehavior) |
Zhenyao Mo | 7211191 | 2016-07-20 17:45:56 -0700 | [diff] [blame] | 221 | { |
Martin Radev | b50ccd3 | 2017-07-06 17:09:58 +0300 | [diff] [blame] | 222 | TIntermBlock *body = FindMainBody(root); |
Olli Etuaho | daaff1c | 2017-07-05 18:03:26 +0300 | [diff] [blame] | 223 | InsertInitCode(body->getSequence(), vars, symbolTable, shaderVersion, extensionBehavior); |
Zhenyao Mo | 7211191 | 2016-07-20 17:45:56 -0700 | [diff] [blame] | 224 | } |
Jamie Madill | 45bcc78 | 2016-11-07 13:58:48 -0500 | [diff] [blame] | 225 | |
| 226 | } // namespace sh |