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 | 9cbc07c | 2017-05-10 18:22:01 +0300 | [diff] [blame] | 18 | #include "compiler/translator/FindMain.h" |
Olli Etuaho | 0ffc441 | 2017-05-19 14:18:55 +0300 | [diff] [blame] | 19 | #include "compiler/translator/InitializeVariables.h" |
Olli Etuaho | 3d932d8 | 2016-04-12 11:10:30 +0300 | [diff] [blame] | 20 | #include "compiler/translator/IntermNode.h" |
Olli Etuaho | a16a84f | 2017-09-12 13:49:18 +0300 | [diff] [blame] | 21 | #include "compiler/translator/IntermNode_util.h" |
Olli Etuaho | 3d932d8 | 2016-04-12 11:10:30 +0300 | [diff] [blame] | 22 | #include "compiler/translator/SymbolTable.h" |
| 23 | |
Jamie Madill | 45bcc78 | 2016-11-07 13:58:48 -0500 | [diff] [blame] | 24 | namespace sh |
| 25 | { |
| 26 | |
Olli Etuaho | 3d932d8 | 2016-04-12 11:10:30 +0300 | [diff] [blame] | 27 | namespace |
| 28 | { |
| 29 | |
Olli Etuaho | 1dd07a6 | 2017-05-12 17:01:22 +0300 | [diff] [blame] | 30 | void GetDeferredInitializers(TIntermDeclaration *declaration, |
Olli Etuaho | 0ffc441 | 2017-05-19 14:18:55 +0300 | [diff] [blame] | 31 | bool initializeUninitializedGlobals, |
Olli Etuaho | 2c7f34c | 2017-10-09 17:18:02 +0300 | [diff] [blame] | 32 | bool canUseLoopsToInitialize, |
Olli Etuaho | 87cc90d | 2017-12-12 15:28:06 +0200 | [diff] [blame^] | 33 | bool highPrecisionSupported, |
Olli Etuaho | 2c7f34c | 2017-10-09 17:18:02 +0300 | [diff] [blame] | 34 | TIntermSequence *deferredInitializersOut, |
| 35 | TSymbolTable *symbolTable) |
Olli Etuaho | 3d932d8 | 2016-04-12 11:10:30 +0300 | [diff] [blame] | 36 | { |
Olli Etuaho | b5601eb | 2017-11-15 18:08:04 +0200 | [diff] [blame] | 37 | // SeparateDeclarations should have already been run. |
| 38 | ASSERT(declaration->getSequence()->size() == 1); |
| 39 | |
| 40 | TIntermNode *declarator = declaration->getSequence()->back(); |
| 41 | TIntermBinary *init = declarator->getAsBinaryNode(); |
| 42 | if (init) |
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 | TIntermSymbol *symbolNode = init->getLeft()->getAsSymbolNode(); |
| 45 | ASSERT(symbolNode); |
| 46 | TIntermTyped *expression = init->getRight(); |
| 47 | |
| 48 | if ((expression->getQualifier() != EvqConst || |
| 49 | (expression->getAsConstantUnion() == nullptr && |
| 50 | !expression->isConstructorWithOnlyConstantUnionParameters()))) |
Olli Etuaho | 3d932d8 | 2016-04-12 11:10:30 +0300 | [diff] [blame] | 51 | { |
Olli Etuaho | b5601eb | 2017-11-15 18:08:04 +0200 | [diff] [blame] | 52 | // For variables which are not constant, defer their real initialization until |
| 53 | // after we initialize uniforms. |
| 54 | // Deferral is done also in any cases where the variable has not been constant |
| 55 | // folded, since otherwise there's a chance that HLSL output will generate extra |
| 56 | // statements from the initializer expression. |
Olli Etuaho | 3d932d8 | 2016-04-12 11:10:30 +0300 | [diff] [blame] | 57 | |
Olli Etuaho | b5601eb | 2017-11-15 18:08:04 +0200 | [diff] [blame] | 58 | // Change const global to a regular global if its initialization is deferred. |
| 59 | // This can happen if ANGLE has not been able to fold the constant expression used |
| 60 | // as an initializer. |
| 61 | ASSERT(symbolNode->getQualifier() == EvqConst || |
| 62 | symbolNode->getQualifier() == EvqGlobal); |
| 63 | if (symbolNode->getQualifier() == EvqConst) |
Olli Etuaho | d4f4c11 | 2016-04-15 15:11:24 +0300 | [diff] [blame] | 64 | { |
Olli Etuaho | b5601eb | 2017-11-15 18:08:04 +0200 | [diff] [blame] | 65 | symbolNode->getTypePointer()->setQualifier(EvqGlobal); |
Olli Etuaho | d4f4c11 | 2016-04-15 15:11:24 +0300 | [diff] [blame] | 66 | } |
Olli Etuaho | b5601eb | 2017-11-15 18:08:04 +0200 | [diff] [blame] | 67 | |
| 68 | TIntermBinary *deferredInit = |
| 69 | new TIntermBinary(EOpAssign, symbolNode->deepCopy(), init->getRight()); |
| 70 | deferredInitializersOut->push_back(deferredInit); |
| 71 | |
| 72 | // Remove the initializer from the global scope and just declare the global instead. |
| 73 | declaration->replaceChildNode(init, symbolNode); |
Olli Etuaho | 3d932d8 | 2016-04-12 11:10:30 +0300 | [diff] [blame] | 74 | } |
Olli Etuaho | b5601eb | 2017-11-15 18:08:04 +0200 | [diff] [blame] | 75 | } |
| 76 | else if (initializeUninitializedGlobals) |
| 77 | { |
| 78 | TIntermSymbol *symbolNode = declarator->getAsSymbolNode(); |
| 79 | ASSERT(symbolNode); |
| 80 | |
| 81 | // Ignore ANGLE internal variables. |
| 82 | if (symbolNode->getName().isInternal()) |
| 83 | return; |
| 84 | |
| 85 | if (symbolNode->getQualifier() == EvqGlobal && symbolNode->getSymbol() != "") |
Olli Etuaho | 0ffc441 | 2017-05-19 14:18:55 +0300 | [diff] [blame] | 86 | { |
Olli Etuaho | 87cc90d | 2017-12-12 15:28:06 +0200 | [diff] [blame^] | 87 | TIntermSequence *initCode = CreateInitCode(symbolNode, canUseLoopsToInitialize, |
| 88 | highPrecisionSupported, symbolTable); |
Olli Etuaho | b5601eb | 2017-11-15 18:08:04 +0200 | [diff] [blame] | 89 | deferredInitializersOut->insert(deferredInitializersOut->end(), initCode->begin(), |
| 90 | initCode->end()); |
Olli Etuaho | 0ffc441 | 2017-05-19 14:18:55 +0300 | [diff] [blame] | 91 | } |
Olli Etuaho | 3d932d8 | 2016-04-12 11:10:30 +0300 | [diff] [blame] | 92 | } |
Olli Etuaho | 3d932d8 | 2016-04-12 11:10:30 +0300 | [diff] [blame] | 93 | } |
| 94 | |
Olli Etuaho | a16a84f | 2017-09-12 13:49:18 +0300 | [diff] [blame] | 95 | void InsertInitCallToMain(TIntermBlock *root, |
| 96 | TIntermSequence *deferredInitializers, |
| 97 | TSymbolTable *symbolTable) |
Olli Etuaho | 3d932d8 | 2016-04-12 11:10:30 +0300 | [diff] [blame] | 98 | { |
Olli Etuaho | 8349843 | 2017-05-19 14:57:23 +0300 | [diff] [blame] | 99 | TIntermBlock *initGlobalsBlock = new TIntermBlock(); |
| 100 | initGlobalsBlock->getSequence()->swap(*deferredInitializers); |
Olli Etuaho | 3d932d8 | 2016-04-12 11:10:30 +0300 | [diff] [blame] | 101 | |
Olli Etuaho | a16a84f | 2017-09-12 13:49:18 +0300 | [diff] [blame] | 102 | TSymbolUniqueId initGlobalsFunctionId(symbolTable); |
| 103 | |
| 104 | const char *kInitGlobalsFunctionName = "initGlobals"; |
| 105 | |
| 106 | TIntermFunctionPrototype *initGlobalsFunctionPrototype = |
| 107 | CreateInternalFunctionPrototypeNode(TType(), kInitGlobalsFunctionName, initGlobalsFunctionId); |
| 108 | root->getSequence()->insert(root->getSequence()->begin(), initGlobalsFunctionPrototype); |
| 109 | TIntermFunctionDefinition *initGlobalsFunctionDefinition = CreateInternalFunctionDefinitionNode( |
| 110 | TType(), kInitGlobalsFunctionName, initGlobalsBlock, initGlobalsFunctionId); |
| 111 | root->appendStatement(initGlobalsFunctionDefinition); |
| 112 | |
| 113 | TIntermAggregate *initGlobalsCall = CreateInternalFunctionCallNode( |
| 114 | TType(), kInitGlobalsFunctionName, initGlobalsFunctionId, new TIntermSequence()); |
| 115 | |
Martin Radev | b50ccd3 | 2017-07-06 17:09:58 +0300 | [diff] [blame] | 116 | TIntermBlock *mainBody = FindMainBody(root); |
Olli Etuaho | a16a84f | 2017-09-12 13:49:18 +0300 | [diff] [blame] | 117 | mainBody->getSequence()->insert(mainBody->getSequence()->begin(), initGlobalsCall); |
Olli Etuaho | 3d932d8 | 2016-04-12 11:10:30 +0300 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | } // namespace |
| 121 | |
Olli Etuaho | a16a84f | 2017-09-12 13:49:18 +0300 | [diff] [blame] | 122 | void DeferGlobalInitializers(TIntermBlock *root, |
| 123 | bool initializeUninitializedGlobals, |
Olli Etuaho | 2c7f34c | 2017-10-09 17:18:02 +0300 | [diff] [blame] | 124 | bool canUseLoopsToInitialize, |
Olli Etuaho | 87cc90d | 2017-12-12 15:28:06 +0200 | [diff] [blame^] | 125 | bool highPrecisionSupported, |
Olli Etuaho | a16a84f | 2017-09-12 13:49:18 +0300 | [diff] [blame] | 126 | TSymbolTable *symbolTable) |
Olli Etuaho | 3d932d8 | 2016-04-12 11:10:30 +0300 | [diff] [blame] | 127 | { |
Olli Etuaho | 1dd07a6 | 2017-05-12 17:01:22 +0300 | [diff] [blame] | 128 | TIntermSequence *deferredInitializers = new TIntermSequence(); |
Olli Etuaho | 3d932d8 | 2016-04-12 11:10:30 +0300 | [diff] [blame] | 129 | |
Olli Etuaho | 1dd07a6 | 2017-05-12 17:01:22 +0300 | [diff] [blame] | 130 | // Loop over all global statements and process the declarations. This is simpler than using a |
| 131 | // traverser. |
Olli Etuaho | 0ffc441 | 2017-05-19 14:18:55 +0300 | [diff] [blame] | 132 | for (TIntermNode *statement : *root->getSequence()) |
Olli Etuaho | 1dd07a6 | 2017-05-12 17:01:22 +0300 | [diff] [blame] | 133 | { |
Olli Etuaho | 0ffc441 | 2017-05-19 14:18:55 +0300 | [diff] [blame] | 134 | TIntermDeclaration *declaration = statement->getAsDeclarationNode(); |
| 135 | if (declaration) |
Olli Etuaho | 1dd07a6 | 2017-05-12 17:01:22 +0300 | [diff] [blame] | 136 | { |
Olli Etuaho | 0ffc441 | 2017-05-19 14:18:55 +0300 | [diff] [blame] | 137 | GetDeferredInitializers(declaration, initializeUninitializedGlobals, |
Olli Etuaho | 87cc90d | 2017-12-12 15:28:06 +0200 | [diff] [blame^] | 138 | canUseLoopsToInitialize, highPrecisionSupported, |
| 139 | deferredInitializers, symbolTable); |
Olli Etuaho | 1dd07a6 | 2017-05-12 17:01:22 +0300 | [diff] [blame] | 140 | } |
| 141 | } |
Olli Etuaho | 3d932d8 | 2016-04-12 11:10:30 +0300 | [diff] [blame] | 142 | |
| 143 | // Add the function with initialization and the call to that. |
Olli Etuaho | 1dd07a6 | 2017-05-12 17:01:22 +0300 | [diff] [blame] | 144 | if (!deferredInitializers->empty()) |
| 145 | { |
Olli Etuaho | a16a84f | 2017-09-12 13:49:18 +0300 | [diff] [blame] | 146 | InsertInitCallToMain(root, deferredInitializers, symbolTable); |
Olli Etuaho | 1dd07a6 | 2017-05-12 17:01:22 +0300 | [diff] [blame] | 147 | } |
Olli Etuaho | 3d932d8 | 2016-04-12 11:10:30 +0300 | [diff] [blame] | 148 | } |
Jamie Madill | 45bcc78 | 2016-11-07 13:58:48 -0500 | [diff] [blame] | 149 | |
| 150 | } // namespace sh |