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 | // |
Olli Etuaho | a16a84f | 2017-09-12 13:49:18 +0300 | [diff] [blame] | 6 | // DeferGlobalInitializers is an AST traverser that moves global initializers into a separate |
| 7 | // function that is called in the beginning of main(). This enables initialization of globals with |
| 8 | // uniforms or non-constant globals, as allowed by the WebGL spec. Some initializers referencing |
| 9 | // non-constants may need to be unfolded into if statements in HLSL - this kind of steps should be |
| 10 | // done after DeferGlobalInitializers is run. Note that it's important that the function definition |
| 11 | // is at the end of the shader, as some globals may be declared after main(). |
Olli Etuaho | 3d932d8 | 2016-04-12 11:10:30 +0300 | [diff] [blame] | 12 | // |
Olli Etuaho | 0ffc441 | 2017-05-19 14:18:55 +0300 | [diff] [blame] | 13 | // It can also initialize all uninitialized globals. |
| 14 | // |
Olli Etuaho | 3d932d8 | 2016-04-12 11:10:30 +0300 | [diff] [blame] | 15 | |
| 16 | #include "compiler/translator/DeferGlobalInitializers.h" |
| 17 | |
Olli Etuaho | cf180fc | 2018-01-04 16:25:40 +0200 | [diff] [blame] | 18 | #include <vector> |
| 19 | |
Olli Etuaho | 9cbc07c | 2017-05-10 18:22:01 +0300 | [diff] [blame] | 20 | #include "compiler/translator/FindMain.h" |
Olli Etuaho | 0ffc441 | 2017-05-19 14:18:55 +0300 | [diff] [blame] | 21 | #include "compiler/translator/InitializeVariables.h" |
Olli Etuaho | 3d932d8 | 2016-04-12 11:10:30 +0300 | [diff] [blame] | 22 | #include "compiler/translator/IntermNode.h" |
Olli Etuaho | a16a84f | 2017-09-12 13:49:18 +0300 | [diff] [blame] | 23 | #include "compiler/translator/IntermNode_util.h" |
Olli Etuaho | cf180fc | 2018-01-04 16:25:40 +0200 | [diff] [blame] | 24 | #include "compiler/translator/ReplaceVariable.h" |
Olli Etuaho | 029e8ca | 2018-02-16 14:06:49 +0200 | [diff] [blame^] | 25 | #include "compiler/translator/StaticType.h" |
Olli Etuaho | 3d932d8 | 2016-04-12 11:10:30 +0300 | [diff] [blame] | 26 | #include "compiler/translator/SymbolTable.h" |
| 27 | |
Jamie Madill | 45bcc78 | 2016-11-07 13:58:48 -0500 | [diff] [blame] | 28 | namespace sh |
| 29 | { |
| 30 | |
Olli Etuaho | 3d932d8 | 2016-04-12 11:10:30 +0300 | [diff] [blame] | 31 | namespace |
| 32 | { |
| 33 | |
Olli Etuaho | fbb1c79 | 2018-01-19 16:26:59 +0200 | [diff] [blame] | 34 | constexpr const ImmutableString kInitGlobalsString("initGlobals"); |
| 35 | |
Olli Etuaho | 1dd07a6 | 2017-05-12 17:01:22 +0300 | [diff] [blame] | 36 | void GetDeferredInitializers(TIntermDeclaration *declaration, |
Olli Etuaho | 0ffc441 | 2017-05-19 14:18:55 +0300 | [diff] [blame] | 37 | bool initializeUninitializedGlobals, |
Olli Etuaho | 2c7f34c | 2017-10-09 17:18:02 +0300 | [diff] [blame] | 38 | bool canUseLoopsToInitialize, |
Olli Etuaho | 87cc90d | 2017-12-12 15:28:06 +0200 | [diff] [blame] | 39 | bool highPrecisionSupported, |
Olli Etuaho | 2c7f34c | 2017-10-09 17:18:02 +0300 | [diff] [blame] | 40 | TIntermSequence *deferredInitializersOut, |
Olli Etuaho | cf180fc | 2018-01-04 16:25:40 +0200 | [diff] [blame] | 41 | std::vector<const TVariable *> *variablesToReplaceOut, |
Olli Etuaho | 2c7f34c | 2017-10-09 17:18:02 +0300 | [diff] [blame] | 42 | TSymbolTable *symbolTable) |
Olli Etuaho | 3d932d8 | 2016-04-12 11:10:30 +0300 | [diff] [blame] | 43 | { |
Olli Etuaho | b5601eb | 2017-11-15 18:08:04 +0200 | [diff] [blame] | 44 | // SeparateDeclarations should have already been run. |
| 45 | ASSERT(declaration->getSequence()->size() == 1); |
| 46 | |
| 47 | TIntermNode *declarator = declaration->getSequence()->back(); |
| 48 | TIntermBinary *init = declarator->getAsBinaryNode(); |
| 49 | if (init) |
Olli Etuaho | 3d932d8 | 2016-04-12 11:10:30 +0300 | [diff] [blame] | 50 | { |
Olli Etuaho | b5601eb | 2017-11-15 18:08:04 +0200 | [diff] [blame] | 51 | TIntermSymbol *symbolNode = init->getLeft()->getAsSymbolNode(); |
| 52 | ASSERT(symbolNode); |
| 53 | TIntermTyped *expression = init->getRight(); |
| 54 | |
Olli Etuaho | ea22b7a | 2018-01-04 17:09:11 +0200 | [diff] [blame] | 55 | if (expression->getQualifier() != EvqConst || !expression->hasConstantValue()) |
Olli Etuaho | 3d932d8 | 2016-04-12 11:10:30 +0300 | [diff] [blame] | 56 | { |
Olli Etuaho | b5601eb | 2017-11-15 18:08:04 +0200 | [diff] [blame] | 57 | // For variables which are not constant, defer their real initialization until |
| 58 | // after we initialize uniforms. |
Olli Etuaho | ea22b7a | 2018-01-04 17:09:11 +0200 | [diff] [blame] | 59 | // Deferral is done also in any cases where the variable can not be converted to a |
| 60 | // constant union, since otherwise there's a chance that HLSL output will generate extra |
Olli Etuaho | b5601eb | 2017-11-15 18:08:04 +0200 | [diff] [blame] | 61 | // statements from the initializer expression. |
Olli Etuaho | 3d932d8 | 2016-04-12 11:10:30 +0300 | [diff] [blame] | 62 | |
Olli Etuaho | b5601eb | 2017-11-15 18:08:04 +0200 | [diff] [blame] | 63 | // Change const global to a regular global if its initialization is deferred. |
| 64 | // This can happen if ANGLE has not been able to fold the constant expression used |
| 65 | // as an initializer. |
| 66 | ASSERT(symbolNode->getQualifier() == EvqConst || |
| 67 | symbolNode->getQualifier() == EvqGlobal); |
| 68 | if (symbolNode->getQualifier() == EvqConst) |
Olli Etuaho | d4f4c11 | 2016-04-15 15:11:24 +0300 | [diff] [blame] | 69 | { |
Olli Etuaho | cf180fc | 2018-01-04 16:25:40 +0200 | [diff] [blame] | 70 | variablesToReplaceOut->push_back(&symbolNode->variable()); |
Olli Etuaho | d4f4c11 | 2016-04-15 15:11:24 +0300 | [diff] [blame] | 71 | } |
Olli Etuaho | b5601eb | 2017-11-15 18:08:04 +0200 | [diff] [blame] | 72 | |
| 73 | TIntermBinary *deferredInit = |
| 74 | new TIntermBinary(EOpAssign, symbolNode->deepCopy(), init->getRight()); |
| 75 | deferredInitializersOut->push_back(deferredInit); |
| 76 | |
| 77 | // Remove the initializer from the global scope and just declare the global instead. |
| 78 | declaration->replaceChildNode(init, symbolNode); |
Olli Etuaho | 3d932d8 | 2016-04-12 11:10:30 +0300 | [diff] [blame] | 79 | } |
Olli Etuaho | b5601eb | 2017-11-15 18:08:04 +0200 | [diff] [blame] | 80 | } |
| 81 | else if (initializeUninitializedGlobals) |
| 82 | { |
| 83 | TIntermSymbol *symbolNode = declarator->getAsSymbolNode(); |
| 84 | ASSERT(symbolNode); |
| 85 | |
Olli Etuaho | 8b5e8fd | 2017-12-15 14:59:15 +0200 | [diff] [blame] | 86 | // Ignore ANGLE internal variables and nameless declarations. |
| 87 | if (symbolNode->variable().symbolType() == SymbolType::AngleInternal || |
| 88 | symbolNode->variable().symbolType() == SymbolType::Empty) |
Olli Etuaho | b5601eb | 2017-11-15 18:08:04 +0200 | [diff] [blame] | 89 | return; |
| 90 | |
Olli Etuaho | 8b5e8fd | 2017-12-15 14:59:15 +0200 | [diff] [blame] | 91 | if (symbolNode->getQualifier() == EvqGlobal) |
Olli Etuaho | 0ffc441 | 2017-05-19 14:18:55 +0300 | [diff] [blame] | 92 | { |
Olli Etuaho | 87cc90d | 2017-12-12 15:28:06 +0200 | [diff] [blame] | 93 | TIntermSequence *initCode = CreateInitCode(symbolNode, canUseLoopsToInitialize, |
| 94 | highPrecisionSupported, symbolTable); |
Olli Etuaho | b5601eb | 2017-11-15 18:08:04 +0200 | [diff] [blame] | 95 | deferredInitializersOut->insert(deferredInitializersOut->end(), initCode->begin(), |
| 96 | initCode->end()); |
Olli Etuaho | 0ffc441 | 2017-05-19 14:18:55 +0300 | [diff] [blame] | 97 | } |
Olli Etuaho | 3d932d8 | 2016-04-12 11:10:30 +0300 | [diff] [blame] | 98 | } |
Olli Etuaho | 3d932d8 | 2016-04-12 11:10:30 +0300 | [diff] [blame] | 99 | } |
| 100 | |
Olli Etuaho | a16a84f | 2017-09-12 13:49:18 +0300 | [diff] [blame] | 101 | void InsertInitCallToMain(TIntermBlock *root, |
| 102 | TIntermSequence *deferredInitializers, |
| 103 | TSymbolTable *symbolTable) |
Olli Etuaho | 3d932d8 | 2016-04-12 11:10:30 +0300 | [diff] [blame] | 104 | { |
Olli Etuaho | 8349843 | 2017-05-19 14:57:23 +0300 | [diff] [blame] | 105 | TIntermBlock *initGlobalsBlock = new TIntermBlock(); |
| 106 | initGlobalsBlock->getSequence()->swap(*deferredInitializers); |
Olli Etuaho | 3d932d8 | 2016-04-12 11:10:30 +0300 | [diff] [blame] | 107 | |
Olli Etuaho | 029e8ca | 2018-02-16 14:06:49 +0200 | [diff] [blame^] | 108 | TFunction *initGlobalsFunction = |
| 109 | new TFunction(symbolTable, kInitGlobalsString, SymbolType::AngleInternal, |
| 110 | StaticType::GetBasic<EbtVoid>(), false); |
Olli Etuaho | a16a84f | 2017-09-12 13:49:18 +0300 | [diff] [blame] | 111 | |
| 112 | TIntermFunctionPrototype *initGlobalsFunctionPrototype = |
Olli Etuaho | 0c37100 | 2017-12-13 17:00:25 +0400 | [diff] [blame] | 113 | CreateInternalFunctionPrototypeNode(*initGlobalsFunction); |
Olli Etuaho | a16a84f | 2017-09-12 13:49:18 +0300 | [diff] [blame] | 114 | root->getSequence()->insert(root->getSequence()->begin(), initGlobalsFunctionPrototype); |
Olli Etuaho | 0c37100 | 2017-12-13 17:00:25 +0400 | [diff] [blame] | 115 | TIntermFunctionDefinition *initGlobalsFunctionDefinition = |
| 116 | CreateInternalFunctionDefinitionNode(*initGlobalsFunction, initGlobalsBlock); |
Olli Etuaho | a16a84f | 2017-09-12 13:49:18 +0300 | [diff] [blame] | 117 | root->appendStatement(initGlobalsFunctionDefinition); |
| 118 | |
Olli Etuaho | 0c37100 | 2017-12-13 17:00:25 +0400 | [diff] [blame] | 119 | TIntermAggregate *initGlobalsCall = |
| 120 | TIntermAggregate::CreateFunctionCall(*initGlobalsFunction, new TIntermSequence()); |
Olli Etuaho | a16a84f | 2017-09-12 13:49:18 +0300 | [diff] [blame] | 121 | |
Martin Radev | b50ccd3 | 2017-07-06 17:09:58 +0300 | [diff] [blame] | 122 | TIntermBlock *mainBody = FindMainBody(root); |
Olli Etuaho | a16a84f | 2017-09-12 13:49:18 +0300 | [diff] [blame] | 123 | mainBody->getSequence()->insert(mainBody->getSequence()->begin(), initGlobalsCall); |
Olli Etuaho | 3d932d8 | 2016-04-12 11:10:30 +0300 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | } // namespace |
| 127 | |
Olli Etuaho | a16a84f | 2017-09-12 13:49:18 +0300 | [diff] [blame] | 128 | void DeferGlobalInitializers(TIntermBlock *root, |
| 129 | bool initializeUninitializedGlobals, |
Olli Etuaho | 2c7f34c | 2017-10-09 17:18:02 +0300 | [diff] [blame] | 130 | bool canUseLoopsToInitialize, |
Olli Etuaho | 87cc90d | 2017-12-12 15:28:06 +0200 | [diff] [blame] | 131 | bool highPrecisionSupported, |
Olli Etuaho | a16a84f | 2017-09-12 13:49:18 +0300 | [diff] [blame] | 132 | TSymbolTable *symbolTable) |
Olli Etuaho | 3d932d8 | 2016-04-12 11:10:30 +0300 | [diff] [blame] | 133 | { |
Olli Etuaho | 1dd07a6 | 2017-05-12 17:01:22 +0300 | [diff] [blame] | 134 | TIntermSequence *deferredInitializers = new TIntermSequence(); |
Olli Etuaho | cf180fc | 2018-01-04 16:25:40 +0200 | [diff] [blame] | 135 | std::vector<const TVariable *> variablesToReplace; |
Olli Etuaho | 3d932d8 | 2016-04-12 11:10:30 +0300 | [diff] [blame] | 136 | |
Olli Etuaho | 1dd07a6 | 2017-05-12 17:01:22 +0300 | [diff] [blame] | 137 | // Loop over all global statements and process the declarations. This is simpler than using a |
| 138 | // traverser. |
Olli Etuaho | 0ffc441 | 2017-05-19 14:18:55 +0300 | [diff] [blame] | 139 | for (TIntermNode *statement : *root->getSequence()) |
Olli Etuaho | 1dd07a6 | 2017-05-12 17:01:22 +0300 | [diff] [blame] | 140 | { |
Olli Etuaho | 0ffc441 | 2017-05-19 14:18:55 +0300 | [diff] [blame] | 141 | TIntermDeclaration *declaration = statement->getAsDeclarationNode(); |
| 142 | if (declaration) |
Olli Etuaho | 1dd07a6 | 2017-05-12 17:01:22 +0300 | [diff] [blame] | 143 | { |
Olli Etuaho | 0ffc441 | 2017-05-19 14:18:55 +0300 | [diff] [blame] | 144 | GetDeferredInitializers(declaration, initializeUninitializedGlobals, |
Olli Etuaho | 87cc90d | 2017-12-12 15:28:06 +0200 | [diff] [blame] | 145 | canUseLoopsToInitialize, highPrecisionSupported, |
Olli Etuaho | cf180fc | 2018-01-04 16:25:40 +0200 | [diff] [blame] | 146 | deferredInitializers, &variablesToReplace, symbolTable); |
Olli Etuaho | 1dd07a6 | 2017-05-12 17:01:22 +0300 | [diff] [blame] | 147 | } |
| 148 | } |
Olli Etuaho | 3d932d8 | 2016-04-12 11:10:30 +0300 | [diff] [blame] | 149 | |
| 150 | // Add the function with initialization and the call to that. |
Olli Etuaho | 1dd07a6 | 2017-05-12 17:01:22 +0300 | [diff] [blame] | 151 | if (!deferredInitializers->empty()) |
| 152 | { |
Olli Etuaho | a16a84f | 2017-09-12 13:49:18 +0300 | [diff] [blame] | 153 | InsertInitCallToMain(root, deferredInitializers, symbolTable); |
Olli Etuaho | 1dd07a6 | 2017-05-12 17:01:22 +0300 | [diff] [blame] | 154 | } |
Olli Etuaho | cf180fc | 2018-01-04 16:25:40 +0200 | [diff] [blame] | 155 | |
| 156 | // Replace constant variables with non-constant global variables. |
| 157 | for (const TVariable *var : variablesToReplace) |
| 158 | { |
Olli Etuaho | b60d30f | 2018-01-16 12:31:06 +0200 | [diff] [blame] | 159 | TType *replacementType = new TType(var->getType()); |
| 160 | replacementType->setQualifier(EvqGlobal); |
Olli Etuaho | cf180fc | 2018-01-04 16:25:40 +0200 | [diff] [blame] | 161 | TVariable *replacement = |
Olli Etuaho | fbb1c79 | 2018-01-19 16:26:59 +0200 | [diff] [blame] | 162 | new TVariable(symbolTable, var->name(), replacementType, var->symbolType()); |
Olli Etuaho | cf180fc | 2018-01-04 16:25:40 +0200 | [diff] [blame] | 163 | ReplaceVariable(root, var, replacement); |
| 164 | } |
Olli Etuaho | 3d932d8 | 2016-04-12 11:10:30 +0300 | [diff] [blame] | 165 | } |
Jamie Madill | 45bcc78 | 2016-11-07 13:58:48 -0500 | [diff] [blame] | 166 | |
| 167 | } // namespace sh |