Olli Etuaho | 3d932d8 | 2016-04-12 11:10:30 +0300 | [diff] [blame] | 1 | // |
| 2 | // Copyright (c) 2016 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 | // DeferGlobalInitializers is an AST traverser that moves global initializers into a function, and |
| 7 | // adds a function call to that function in the beginning of main(). |
| 8 | // This enables initialization of globals with uniforms or non-constant globals, as allowed by |
| 9 | // the WebGL spec. Some initializers referencing non-constants may need to be unfolded into if |
| 10 | // statements in HLSL - this kind of steps should be done after DeferGlobalInitializers is run. |
| 11 | // |
| 12 | |
| 13 | #include "compiler/translator/DeferGlobalInitializers.h" |
| 14 | |
| 15 | #include "compiler/translator/IntermNode.h" |
| 16 | #include "compiler/translator/SymbolTable.h" |
| 17 | |
| 18 | namespace |
| 19 | { |
| 20 | |
| 21 | void SetInternalFunctionName(TIntermAggregate *functionNode, const char *name) |
| 22 | { |
| 23 | TString nameStr(name); |
| 24 | nameStr = TFunction::mangleName(nameStr); |
| 25 | TName nameObj(nameStr); |
| 26 | nameObj.setInternal(true); |
| 27 | functionNode->setNameObj(nameObj); |
| 28 | } |
| 29 | |
Olli Etuaho | d4f4c11 | 2016-04-15 15:11:24 +0300 | [diff] [blame] | 30 | TIntermAggregate *CreateFunctionPrototypeNode(const char *name, const int functionId) |
Olli Etuaho | 3d932d8 | 2016-04-12 11:10:30 +0300 | [diff] [blame] | 31 | { |
| 32 | TIntermAggregate *functionNode = new TIntermAggregate(EOpPrototype); |
| 33 | |
| 34 | SetInternalFunctionName(functionNode, name); |
| 35 | TType returnType(EbtVoid); |
| 36 | functionNode->setType(returnType); |
Olli Etuaho | d4f4c11 | 2016-04-15 15:11:24 +0300 | [diff] [blame] | 37 | functionNode->setFunctionId(functionId); |
Olli Etuaho | 3d932d8 | 2016-04-12 11:10:30 +0300 | [diff] [blame] | 38 | return functionNode; |
| 39 | } |
| 40 | |
Olli Etuaho | d4f4c11 | 2016-04-15 15:11:24 +0300 | [diff] [blame] | 41 | TIntermAggregate *CreateFunctionDefinitionNode(const char *name, |
| 42 | TIntermAggregate *functionBody, |
| 43 | const int functionId) |
Olli Etuaho | 3d932d8 | 2016-04-12 11:10:30 +0300 | [diff] [blame] | 44 | { |
| 45 | TIntermAggregate *functionNode = new TIntermAggregate(EOpFunction); |
| 46 | TIntermAggregate *paramsNode = new TIntermAggregate(EOpParameters); |
| 47 | functionNode->getSequence()->push_back(paramsNode); |
| 48 | functionNode->getSequence()->push_back(functionBody); |
| 49 | |
| 50 | SetInternalFunctionName(functionNode, name); |
| 51 | TType returnType(EbtVoid); |
| 52 | functionNode->setType(returnType); |
Olli Etuaho | d4f4c11 | 2016-04-15 15:11:24 +0300 | [diff] [blame] | 53 | functionNode->setFunctionId(functionId); |
Olli Etuaho | 3d932d8 | 2016-04-12 11:10:30 +0300 | [diff] [blame] | 54 | return functionNode; |
| 55 | } |
| 56 | |
Olli Etuaho | d4f4c11 | 2016-04-15 15:11:24 +0300 | [diff] [blame] | 57 | TIntermAggregate *CreateFunctionCallNode(const char *name, const int functionId) |
Olli Etuaho | 3d932d8 | 2016-04-12 11:10:30 +0300 | [diff] [blame] | 58 | { |
| 59 | TIntermAggregate *functionNode = new TIntermAggregate(EOpFunctionCall); |
| 60 | |
Olli Etuaho | d4f4c11 | 2016-04-15 15:11:24 +0300 | [diff] [blame] | 61 | functionNode->setUserDefined(); |
Olli Etuaho | 3d932d8 | 2016-04-12 11:10:30 +0300 | [diff] [blame] | 62 | SetInternalFunctionName(functionNode, name); |
| 63 | TType returnType(EbtVoid); |
| 64 | functionNode->setType(returnType); |
Olli Etuaho | d4f4c11 | 2016-04-15 15:11:24 +0300 | [diff] [blame] | 65 | functionNode->setFunctionId(functionId); |
Olli Etuaho | 3d932d8 | 2016-04-12 11:10:30 +0300 | [diff] [blame] | 66 | return functionNode; |
| 67 | } |
| 68 | |
| 69 | class DeferGlobalInitializersTraverser : public TIntermTraverser |
| 70 | { |
| 71 | public: |
| 72 | DeferGlobalInitializersTraverser(); |
| 73 | |
| 74 | bool visitBinary(Visit visit, TIntermBinary *node) override; |
| 75 | |
| 76 | void insertInitFunction(TIntermNode *root); |
| 77 | |
| 78 | private: |
| 79 | TIntermSequence mDeferredInitializers; |
| 80 | }; |
| 81 | |
| 82 | DeferGlobalInitializersTraverser::DeferGlobalInitializersTraverser() |
| 83 | : TIntermTraverser(true, false, false) |
| 84 | { |
| 85 | } |
| 86 | |
| 87 | bool DeferGlobalInitializersTraverser::visitBinary(Visit visit, TIntermBinary *node) |
| 88 | { |
| 89 | if (node->getOp() == EOpInitialize) |
| 90 | { |
| 91 | TIntermSymbol *symbolNode = node->getLeft()->getAsSymbolNode(); |
| 92 | ASSERT(symbolNode); |
| 93 | TIntermTyped *expression = node->getRight(); |
| 94 | |
Olli Etuaho | d4f4c11 | 2016-04-15 15:11:24 +0300 | [diff] [blame] | 95 | if (mInGlobalScope && (expression->getQualifier() != EvqConst || |
| 96 | (expression->getAsConstantUnion() == nullptr && |
| 97 | !expression->isConstructorWithOnlyConstantUnionParameters()))) |
Olli Etuaho | 3d932d8 | 2016-04-12 11:10:30 +0300 | [diff] [blame] | 98 | { |
| 99 | // For variables which are not constant, defer their real initialization until |
| 100 | // after we initialize uniforms. |
| 101 | // Deferral is done also in any cases where the variable has not been constant folded, |
| 102 | // since otherwise there's a chance that HLSL output will generate extra statements |
| 103 | // from the initializer expression. |
| 104 | TIntermBinary *deferredInit = new TIntermBinary(EOpAssign); |
Olli Etuaho | d4f4c11 | 2016-04-15 15:11:24 +0300 | [diff] [blame] | 105 | deferredInit->setLeft(symbolNode->deepCopy()); |
Olli Etuaho | 3d932d8 | 2016-04-12 11:10:30 +0300 | [diff] [blame] | 106 | deferredInit->setRight(node->getRight()); |
| 107 | deferredInit->setType(node->getType()); |
| 108 | mDeferredInitializers.push_back(deferredInit); |
| 109 | |
Olli Etuaho | d4f4c11 | 2016-04-15 15:11:24 +0300 | [diff] [blame] | 110 | // Change const global to a regular global if its initialization is deferred. |
| 111 | // This can happen if ANGLE has not been able to fold the constant expression used |
| 112 | // as an initializer. |
| 113 | ASSERT(symbolNode->getQualifier() == EvqConst || |
| 114 | symbolNode->getQualifier() == EvqGlobal); |
| 115 | if (symbolNode->getQualifier() == EvqConst) |
| 116 | { |
| 117 | // All of the siblings in the same declaration need to have consistent qualifiers. |
| 118 | auto *siblings = getParentNode()->getAsAggregate()->getSequence(); |
| 119 | for (TIntermNode *siblingNode : *siblings) |
| 120 | { |
| 121 | TIntermBinary *siblingBinary = siblingNode->getAsBinaryNode(); |
| 122 | if (siblingBinary) |
| 123 | { |
| 124 | ASSERT(siblingBinary->getOp() == EOpInitialize); |
| 125 | siblingBinary->getLeft()->getTypePointer()->setQualifier(EvqGlobal); |
| 126 | } |
| 127 | siblingNode->getAsTyped()->getTypePointer()->setQualifier(EvqGlobal); |
| 128 | } |
| 129 | // This node is one of the siblings. |
| 130 | ASSERT(symbolNode->getQualifier() == EvqGlobal); |
| 131 | } |
Olli Etuaho | 3d932d8 | 2016-04-12 11:10:30 +0300 | [diff] [blame] | 132 | // Remove the initializer from the global scope and just declare the global instead. |
Jamie Madill | 03d863c | 2016-07-27 18:15:53 -0400 | [diff] [blame^] | 133 | queueReplacement(node, symbolNode, OriginalNode::IS_DROPPED); |
Olli Etuaho | 3d932d8 | 2016-04-12 11:10:30 +0300 | [diff] [blame] | 134 | } |
| 135 | } |
| 136 | return false; |
| 137 | } |
| 138 | |
| 139 | void DeferGlobalInitializersTraverser::insertInitFunction(TIntermNode *root) |
| 140 | { |
| 141 | if (mDeferredInitializers.empty()) |
| 142 | { |
| 143 | return; |
| 144 | } |
Olli Etuaho | d4f4c11 | 2016-04-15 15:11:24 +0300 | [diff] [blame] | 145 | const int initFunctionId = TSymbolTable::nextUniqueId(); |
Olli Etuaho | 3d932d8 | 2016-04-12 11:10:30 +0300 | [diff] [blame] | 146 | TIntermAggregate *rootAgg = root->getAsAggregate(); |
| 147 | ASSERT(rootAgg != nullptr && rootAgg->getOp() == EOpSequence); |
| 148 | |
| 149 | const char *functionName = "initializeDeferredGlobals"; |
| 150 | |
| 151 | // Add function prototype to the beginning of the shader |
Olli Etuaho | d4f4c11 | 2016-04-15 15:11:24 +0300 | [diff] [blame] | 152 | TIntermAggregate *functionPrototypeNode = |
| 153 | CreateFunctionPrototypeNode(functionName, initFunctionId); |
Olli Etuaho | 3d932d8 | 2016-04-12 11:10:30 +0300 | [diff] [blame] | 154 | rootAgg->getSequence()->insert(rootAgg->getSequence()->begin(), functionPrototypeNode); |
| 155 | |
| 156 | // Add function definition to the end of the shader |
| 157 | TIntermAggregate *functionBodyNode = new TIntermAggregate(EOpSequence); |
| 158 | TIntermSequence *functionBody = functionBodyNode->getSequence(); |
| 159 | for (const auto &deferredInit : mDeferredInitializers) |
| 160 | { |
| 161 | functionBody->push_back(deferredInit); |
| 162 | } |
| 163 | TIntermAggregate *functionDefinition = |
Olli Etuaho | d4f4c11 | 2016-04-15 15:11:24 +0300 | [diff] [blame] | 164 | CreateFunctionDefinitionNode(functionName, functionBodyNode, initFunctionId); |
Olli Etuaho | 3d932d8 | 2016-04-12 11:10:30 +0300 | [diff] [blame] | 165 | rootAgg->getSequence()->push_back(functionDefinition); |
| 166 | |
| 167 | // Insert call into main function |
| 168 | for (TIntermNode *node : *rootAgg->getSequence()) |
| 169 | { |
| 170 | TIntermAggregate *nodeAgg = node->getAsAggregate(); |
| 171 | if (nodeAgg != nullptr && nodeAgg->getOp() == EOpFunction && |
| 172 | TFunction::unmangleName(nodeAgg->getName()) == "main") |
| 173 | { |
Olli Etuaho | d4f4c11 | 2016-04-15 15:11:24 +0300 | [diff] [blame] | 174 | TIntermAggregate *functionCallNode = |
| 175 | CreateFunctionCallNode(functionName, initFunctionId); |
Olli Etuaho | 3d932d8 | 2016-04-12 11:10:30 +0300 | [diff] [blame] | 176 | |
| 177 | TIntermNode *mainBody = nodeAgg->getSequence()->back(); |
| 178 | TIntermAggregate *mainBodyAgg = mainBody->getAsAggregate(); |
| 179 | ASSERT(mainBodyAgg != nullptr && mainBodyAgg->getOp() == EOpSequence); |
| 180 | mainBodyAgg->getSequence()->insert(mainBodyAgg->getSequence()->begin(), |
| 181 | functionCallNode); |
| 182 | } |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | } // namespace |
| 187 | |
| 188 | void DeferGlobalInitializers(TIntermNode *root) |
| 189 | { |
| 190 | DeferGlobalInitializersTraverser traverser; |
| 191 | root->traverse(&traverser); |
| 192 | |
| 193 | // Replace the initializers of the global variables. |
| 194 | traverser.updateTree(); |
| 195 | |
| 196 | // Add the function with initialization and the call to that. |
| 197 | traverser.insertInitFunction(root); |
| 198 | } |