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 | |
Olli Etuaho | 9cbc07c | 2017-05-10 18:22:01 +0300 | [diff] [blame] | 15 | #include "compiler/translator/FindMain.h" |
Olli Etuaho | 3d932d8 | 2016-04-12 11:10:30 +0300 | [diff] [blame] | 16 | #include "compiler/translator/IntermNode.h" |
| 17 | #include "compiler/translator/SymbolTable.h" |
| 18 | |
Jamie Madill | 45bcc78 | 2016-11-07 13:58:48 -0500 | [diff] [blame] | 19 | namespace sh |
| 20 | { |
| 21 | |
Olli Etuaho | 3d932d8 | 2016-04-12 11:10:30 +0300 | [diff] [blame] | 22 | namespace |
| 23 | { |
| 24 | |
Olli Etuaho | 1dd07a6 | 2017-05-12 17:01:22 +0300 | [diff] [blame^] | 25 | void GetDeferredInitializers(TIntermDeclaration *declaration, |
| 26 | TIntermSequence *deferredInitializersOut) |
Olli Etuaho | 3d932d8 | 2016-04-12 11:10:30 +0300 | [diff] [blame] | 27 | { |
Olli Etuaho | 1dd07a6 | 2017-05-12 17:01:22 +0300 | [diff] [blame^] | 28 | // We iterate with an index instead of using an iterator since we're replacing the children of |
| 29 | // declaration inside the loop. |
| 30 | for (size_t i = 0; i < declaration->getSequence()->size(); ++i) |
Olli Etuaho | 3d932d8 | 2016-04-12 11:10:30 +0300 | [diff] [blame] | 31 | { |
Olli Etuaho | 1dd07a6 | 2017-05-12 17:01:22 +0300 | [diff] [blame^] | 32 | TIntermNode *declarator = declaration->getSequence()->at(i); |
| 33 | TIntermBinary *init = declarator->getAsBinaryNode(); |
| 34 | if (init) |
Olli Etuaho | 3d932d8 | 2016-04-12 11:10:30 +0300 | [diff] [blame] | 35 | { |
Olli Etuaho | 1dd07a6 | 2017-05-12 17:01:22 +0300 | [diff] [blame^] | 36 | TIntermSymbol *symbolNode = init->getLeft()->getAsSymbolNode(); |
| 37 | ASSERT(symbolNode); |
| 38 | TIntermTyped *expression = init->getRight(); |
Olli Etuaho | 3d932d8 | 2016-04-12 11:10:30 +0300 | [diff] [blame] | 39 | |
Olli Etuaho | 1dd07a6 | 2017-05-12 17:01:22 +0300 | [diff] [blame^] | 40 | if ((expression->getQualifier() != EvqConst || |
| 41 | (expression->getAsConstantUnion() == nullptr && |
| 42 | !expression->isConstructorWithOnlyConstantUnionParameters()))) |
Olli Etuaho | d4f4c11 | 2016-04-15 15:11:24 +0300 | [diff] [blame] | 43 | { |
Olli Etuaho | 1dd07a6 | 2017-05-12 17:01:22 +0300 | [diff] [blame^] | 44 | // For variables which are not constant, defer their real initialization until |
| 45 | // after we initialize uniforms. |
| 46 | // Deferral is done also in any cases where the variable has not been constant |
| 47 | // folded, since otherwise there's a chance that HLSL output will generate extra |
| 48 | // statements from the initializer expression. |
| 49 | TIntermBinary *deferredInit = |
| 50 | new TIntermBinary(EOpAssign, symbolNode->deepCopy(), init->getRight()); |
| 51 | deferredInitializersOut->push_back(deferredInit); |
| 52 | |
| 53 | // Change const global to a regular global if its initialization is deferred. |
| 54 | // This can happen if ANGLE has not been able to fold the constant expression used |
| 55 | // as an initializer. |
| 56 | ASSERT(symbolNode->getQualifier() == EvqConst || |
| 57 | symbolNode->getQualifier() == EvqGlobal); |
| 58 | if (symbolNode->getQualifier() == EvqConst) |
Olli Etuaho | d4f4c11 | 2016-04-15 15:11:24 +0300 | [diff] [blame] | 59 | { |
Olli Etuaho | 1dd07a6 | 2017-05-12 17:01:22 +0300 | [diff] [blame^] | 60 | // All of the siblings in the same declaration need to have consistent |
| 61 | // qualifiers. |
| 62 | auto *siblings = declaration->getSequence(); |
| 63 | for (TIntermNode *siblingNode : *siblings) |
Olli Etuaho | d4f4c11 | 2016-04-15 15:11:24 +0300 | [diff] [blame] | 64 | { |
Olli Etuaho | 1dd07a6 | 2017-05-12 17:01:22 +0300 | [diff] [blame^] | 65 | TIntermBinary *siblingBinary = siblingNode->getAsBinaryNode(); |
| 66 | if (siblingBinary) |
| 67 | { |
| 68 | ASSERT(siblingBinary->getOp() == EOpInitialize); |
| 69 | siblingBinary->getLeft()->getTypePointer()->setQualifier(EvqGlobal); |
| 70 | } |
| 71 | siblingNode->getAsTyped()->getTypePointer()->setQualifier(EvqGlobal); |
Olli Etuaho | d4f4c11 | 2016-04-15 15:11:24 +0300 | [diff] [blame] | 72 | } |
Olli Etuaho | 1dd07a6 | 2017-05-12 17:01:22 +0300 | [diff] [blame^] | 73 | // This node is one of the siblings. |
| 74 | ASSERT(symbolNode->getQualifier() == EvqGlobal); |
Olli Etuaho | d4f4c11 | 2016-04-15 15:11:24 +0300 | [diff] [blame] | 75 | } |
Olli Etuaho | 1dd07a6 | 2017-05-12 17:01:22 +0300 | [diff] [blame^] | 76 | // Remove the initializer from the global scope and just declare the global instead. |
| 77 | declaration->replaceChildNode(init, symbolNode); |
Olli Etuaho | d4f4c11 | 2016-04-15 15:11:24 +0300 | [diff] [blame] | 78 | } |
Olli Etuaho | 3d932d8 | 2016-04-12 11:10:30 +0300 | [diff] [blame] | 79 | } |
| 80 | } |
Olli Etuaho | 3d932d8 | 2016-04-12 11:10:30 +0300 | [diff] [blame] | 81 | } |
| 82 | |
Olli Etuaho | 1dd07a6 | 2017-05-12 17:01:22 +0300 | [diff] [blame^] | 83 | void InsertInitFunction(TIntermBlock *root, TIntermSequence *deferredInitializers) |
Olli Etuaho | 3d932d8 | 2016-04-12 11:10:30 +0300 | [diff] [blame] | 84 | { |
Olli Etuaho | fe48632 | 2017-03-21 09:30:54 +0000 | [diff] [blame] | 85 | TSymbolUniqueId initFunctionId; |
Olli Etuaho | 1dd07a6 | 2017-05-12 17:01:22 +0300 | [diff] [blame^] | 86 | const char *functionName = "initializeGlobals"; |
Olli Etuaho | 3d932d8 | 2016-04-12 11:10:30 +0300 | [diff] [blame] | 87 | |
| 88 | // Add function prototype to the beginning of the shader |
Olli Etuaho | 16c745a | 2017-01-16 17:02:27 +0000 | [diff] [blame] | 89 | TIntermFunctionPrototype *functionPrototypeNode = |
Olli Etuaho | 1dd07a6 | 2017-05-12 17:01:22 +0300 | [diff] [blame^] | 90 | TIntermTraverser::CreateInternalFunctionPrototypeNode(TType(EbtVoid), functionName, |
| 91 | initFunctionId); |
Olli Etuaho | 6d40bbd | 2016-09-30 13:49:38 +0100 | [diff] [blame] | 92 | root->getSequence()->insert(root->getSequence()->begin(), functionPrototypeNode); |
Olli Etuaho | 3d932d8 | 2016-04-12 11:10:30 +0300 | [diff] [blame] | 93 | |
| 94 | // Add function definition to the end of the shader |
Olli Etuaho | 6d40bbd | 2016-09-30 13:49:38 +0100 | [diff] [blame] | 95 | TIntermBlock *functionBodyNode = new TIntermBlock(); |
Olli Etuaho | 1dd07a6 | 2017-05-12 17:01:22 +0300 | [diff] [blame^] | 96 | functionBodyNode->getSequence()->swap(*deferredInitializers); |
| 97 | TIntermFunctionDefinition *functionDefinition = |
| 98 | TIntermTraverser::CreateInternalFunctionDefinitionNode(TType(EbtVoid), functionName, |
| 99 | functionBodyNode, initFunctionId); |
Olli Etuaho | 6d40bbd | 2016-09-30 13:49:38 +0100 | [diff] [blame] | 100 | root->getSequence()->push_back(functionDefinition); |
Olli Etuaho | 3d932d8 | 2016-04-12 11:10:30 +0300 | [diff] [blame] | 101 | |
| 102 | // Insert call into main function |
Olli Etuaho | 9cbc07c | 2017-05-10 18:22:01 +0300 | [diff] [blame] | 103 | TIntermFunctionDefinition *main = FindMain(root); |
| 104 | ASSERT(main != nullptr); |
Olli Etuaho | 1dd07a6 | 2017-05-12 17:01:22 +0300 | [diff] [blame^] | 105 | TIntermAggregate *functionCallNode = TIntermTraverser::CreateInternalFunctionCallNode( |
| 106 | TType(EbtVoid), functionName, initFunctionId, nullptr); |
Olli Etuaho | 3d932d8 | 2016-04-12 11:10:30 +0300 | [diff] [blame] | 107 | |
Olli Etuaho | 9cbc07c | 2017-05-10 18:22:01 +0300 | [diff] [blame] | 108 | TIntermBlock *mainBody = main->getBody(); |
| 109 | ASSERT(mainBody != nullptr); |
| 110 | mainBody->getSequence()->insert(mainBody->getSequence()->begin(), functionCallNode); |
Olli Etuaho | 3d932d8 | 2016-04-12 11:10:30 +0300 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | } // namespace |
| 114 | |
Olli Etuaho | 6d40bbd | 2016-09-30 13:49:38 +0100 | [diff] [blame] | 115 | void DeferGlobalInitializers(TIntermBlock *root) |
Olli Etuaho | 3d932d8 | 2016-04-12 11:10:30 +0300 | [diff] [blame] | 116 | { |
Olli Etuaho | 1dd07a6 | 2017-05-12 17:01:22 +0300 | [diff] [blame^] | 117 | TIntermSequence *deferredInitializers = new TIntermSequence(); |
Olli Etuaho | 3d932d8 | 2016-04-12 11:10:30 +0300 | [diff] [blame] | 118 | |
Olli Etuaho | 1dd07a6 | 2017-05-12 17:01:22 +0300 | [diff] [blame^] | 119 | // Loop over all global statements and process the declarations. This is simpler than using a |
| 120 | // traverser. |
| 121 | for (TIntermNode *declaration : *root->getSequence()) |
| 122 | { |
| 123 | TIntermDeclaration *asVariableDeclaration = declaration->getAsDeclarationNode(); |
| 124 | if (asVariableDeclaration) |
| 125 | { |
| 126 | GetDeferredInitializers(asVariableDeclaration, deferredInitializers); |
| 127 | } |
| 128 | } |
Olli Etuaho | 3d932d8 | 2016-04-12 11:10:30 +0300 | [diff] [blame] | 129 | |
| 130 | // Add the function with initialization and the call to that. |
Olli Etuaho | 1dd07a6 | 2017-05-12 17:01:22 +0300 | [diff] [blame^] | 131 | if (!deferredInitializers->empty()) |
| 132 | { |
| 133 | InsertInitFunction(root, deferredInitializers); |
| 134 | } |
Olli Etuaho | 3d932d8 | 2016-04-12 11:10:30 +0300 | [diff] [blame] | 135 | } |
Jamie Madill | 45bcc78 | 2016-11-07 13:58:48 -0500 | [diff] [blame] | 136 | |
| 137 | } // namespace sh |