blob: 27ff28124cd804298802f25022a64a444cb4c5bf [file] [log] [blame]
Olli Etuaho3d932d82016-04-12 11:10:30 +03001//
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 Etuahoa16a84f2017-09-12 13:49:18 +03006// 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 Etuaho3d932d82016-04-12 11:10:30 +030012//
Olli Etuaho0ffc4412017-05-19 14:18:55 +030013// It can also initialize all uninitialized globals.
14//
Olli Etuaho3d932d82016-04-12 11:10:30 +030015
16#include "compiler/translator/DeferGlobalInitializers.h"
17
Olli Etuaho9cbc07c2017-05-10 18:22:01 +030018#include "compiler/translator/FindMain.h"
Olli Etuaho0ffc4412017-05-19 14:18:55 +030019#include "compiler/translator/InitializeVariables.h"
Olli Etuaho3d932d82016-04-12 11:10:30 +030020#include "compiler/translator/IntermNode.h"
Olli Etuahoa16a84f2017-09-12 13:49:18 +030021#include "compiler/translator/IntermNode_util.h"
Olli Etuaho3d932d82016-04-12 11:10:30 +030022#include "compiler/translator/SymbolTable.h"
23
Jamie Madill45bcc782016-11-07 13:58:48 -050024namespace sh
25{
26
Olli Etuaho3d932d82016-04-12 11:10:30 +030027namespace
28{
29
Olli Etuaho1dd07a62017-05-12 17:01:22 +030030void GetDeferredInitializers(TIntermDeclaration *declaration,
Olli Etuaho0ffc4412017-05-19 14:18:55 +030031 bool initializeUninitializedGlobals,
Olli Etuaho2c7f34c2017-10-09 17:18:02 +030032 bool canUseLoopsToInitialize,
Olli Etuaho87cc90d2017-12-12 15:28:06 +020033 bool highPrecisionSupported,
Olli Etuaho2c7f34c2017-10-09 17:18:02 +030034 TIntermSequence *deferredInitializersOut,
35 TSymbolTable *symbolTable)
Olli Etuaho3d932d82016-04-12 11:10:30 +030036{
Olli Etuahob5601eb2017-11-15 18:08:04 +020037 // 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 Etuaho3d932d82016-04-12 11:10:30 +030043 {
Olli Etuahob5601eb2017-11-15 18:08:04 +020044 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 Etuaho3d932d82016-04-12 11:10:30 +030051 {
Olli Etuahob5601eb2017-11-15 18:08:04 +020052 // 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 Etuaho3d932d82016-04-12 11:10:30 +030057
Olli Etuahob5601eb2017-11-15 18:08:04 +020058 // 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 Etuahod4f4c112016-04-15 15:11:24 +030064 {
Olli Etuahob5601eb2017-11-15 18:08:04 +020065 symbolNode->getTypePointer()->setQualifier(EvqGlobal);
Olli Etuahod4f4c112016-04-15 15:11:24 +030066 }
Olli Etuahob5601eb2017-11-15 18:08:04 +020067
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 Etuaho3d932d82016-04-12 11:10:30 +030074 }
Olli Etuahob5601eb2017-11-15 18:08:04 +020075 }
76 else if (initializeUninitializedGlobals)
77 {
78 TIntermSymbol *symbolNode = declarator->getAsSymbolNode();
79 ASSERT(symbolNode);
80
Olli Etuaho8b5e8fd2017-12-15 14:59:15 +020081 // Ignore ANGLE internal variables and nameless declarations.
82 if (symbolNode->variable().symbolType() == SymbolType::AngleInternal ||
83 symbolNode->variable().symbolType() == SymbolType::Empty)
Olli Etuahob5601eb2017-11-15 18:08:04 +020084 return;
85
Olli Etuaho8b5e8fd2017-12-15 14:59:15 +020086 if (symbolNode->getQualifier() == EvqGlobal)
Olli Etuaho0ffc4412017-05-19 14:18:55 +030087 {
Olli Etuaho87cc90d2017-12-12 15:28:06 +020088 TIntermSequence *initCode = CreateInitCode(symbolNode, canUseLoopsToInitialize,
89 highPrecisionSupported, symbolTable);
Olli Etuahob5601eb2017-11-15 18:08:04 +020090 deferredInitializersOut->insert(deferredInitializersOut->end(), initCode->begin(),
91 initCode->end());
Olli Etuaho0ffc4412017-05-19 14:18:55 +030092 }
Olli Etuaho3d932d82016-04-12 11:10:30 +030093 }
Olli Etuaho3d932d82016-04-12 11:10:30 +030094}
95
Olli Etuahoa16a84f2017-09-12 13:49:18 +030096void InsertInitCallToMain(TIntermBlock *root,
97 TIntermSequence *deferredInitializers,
98 TSymbolTable *symbolTable)
Olli Etuaho3d932d82016-04-12 11:10:30 +030099{
Olli Etuaho83498432017-05-19 14:57:23 +0300100 TIntermBlock *initGlobalsBlock = new TIntermBlock();
101 initGlobalsBlock->getSequence()->swap(*deferredInitializers);
Olli Etuaho3d932d82016-04-12 11:10:30 +0300102
Olli Etuaho0c371002017-12-13 17:00:25 +0400103 TFunction *initGlobalsFunction =
104 new TFunction(symbolTable, NewPoolTString("initGlobals"), new TType(EbtVoid),
105 SymbolType::AngleInternal, false);
Olli Etuahoa16a84f2017-09-12 13:49:18 +0300106
107 TIntermFunctionPrototype *initGlobalsFunctionPrototype =
Olli Etuaho0c371002017-12-13 17:00:25 +0400108 CreateInternalFunctionPrototypeNode(*initGlobalsFunction);
Olli Etuahoa16a84f2017-09-12 13:49:18 +0300109 root->getSequence()->insert(root->getSequence()->begin(), initGlobalsFunctionPrototype);
Olli Etuaho0c371002017-12-13 17:00:25 +0400110 TIntermFunctionDefinition *initGlobalsFunctionDefinition =
111 CreateInternalFunctionDefinitionNode(*initGlobalsFunction, initGlobalsBlock);
Olli Etuahoa16a84f2017-09-12 13:49:18 +0300112 root->appendStatement(initGlobalsFunctionDefinition);
113
Olli Etuaho0c371002017-12-13 17:00:25 +0400114 TIntermAggregate *initGlobalsCall =
115 TIntermAggregate::CreateFunctionCall(*initGlobalsFunction, new TIntermSequence());
Olli Etuahoa16a84f2017-09-12 13:49:18 +0300116
Martin Radevb50ccd32017-07-06 17:09:58 +0300117 TIntermBlock *mainBody = FindMainBody(root);
Olli Etuahoa16a84f2017-09-12 13:49:18 +0300118 mainBody->getSequence()->insert(mainBody->getSequence()->begin(), initGlobalsCall);
Olli Etuaho3d932d82016-04-12 11:10:30 +0300119}
120
121} // namespace
122
Olli Etuahoa16a84f2017-09-12 13:49:18 +0300123void DeferGlobalInitializers(TIntermBlock *root,
124 bool initializeUninitializedGlobals,
Olli Etuaho2c7f34c2017-10-09 17:18:02 +0300125 bool canUseLoopsToInitialize,
Olli Etuaho87cc90d2017-12-12 15:28:06 +0200126 bool highPrecisionSupported,
Olli Etuahoa16a84f2017-09-12 13:49:18 +0300127 TSymbolTable *symbolTable)
Olli Etuaho3d932d82016-04-12 11:10:30 +0300128{
Olli Etuaho1dd07a62017-05-12 17:01:22 +0300129 TIntermSequence *deferredInitializers = new TIntermSequence();
Olli Etuaho3d932d82016-04-12 11:10:30 +0300130
Olli Etuaho1dd07a62017-05-12 17:01:22 +0300131 // Loop over all global statements and process the declarations. This is simpler than using a
132 // traverser.
Olli Etuaho0ffc4412017-05-19 14:18:55 +0300133 for (TIntermNode *statement : *root->getSequence())
Olli Etuaho1dd07a62017-05-12 17:01:22 +0300134 {
Olli Etuaho0ffc4412017-05-19 14:18:55 +0300135 TIntermDeclaration *declaration = statement->getAsDeclarationNode();
136 if (declaration)
Olli Etuaho1dd07a62017-05-12 17:01:22 +0300137 {
Olli Etuaho0ffc4412017-05-19 14:18:55 +0300138 GetDeferredInitializers(declaration, initializeUninitializedGlobals,
Olli Etuaho87cc90d2017-12-12 15:28:06 +0200139 canUseLoopsToInitialize, highPrecisionSupported,
140 deferredInitializers, symbolTable);
Olli Etuaho1dd07a62017-05-12 17:01:22 +0300141 }
142 }
Olli Etuaho3d932d82016-04-12 11:10:30 +0300143
144 // Add the function with initialization and the call to that.
Olli Etuaho1dd07a62017-05-12 17:01:22 +0300145 if (!deferredInitializers->empty())
146 {
Olli Etuahoa16a84f2017-09-12 13:49:18 +0300147 InsertInitCallToMain(root, deferredInitializers, symbolTable);
Olli Etuaho1dd07a62017-05-12 17:01:22 +0300148 }
Olli Etuaho3d932d82016-04-12 11:10:30 +0300149}
Jamie Madill45bcc782016-11-07 13:58:48 -0500150
151} // namespace sh