blob: ef95bc69eb2e458cd07befee10d019958a6d0bf0 [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
Olli Etuahoea22b7a2018-01-04 17:09:11 +020048 if (expression->getQualifier() != EvqConst || !expression->hasConstantValue())
Olli Etuaho3d932d82016-04-12 11:10:30 +030049 {
Olli Etuahob5601eb2017-11-15 18:08:04 +020050 // For variables which are not constant, defer their real initialization until
51 // after we initialize uniforms.
Olli Etuahoea22b7a2018-01-04 17:09:11 +020052 // Deferral is done also in any cases where the variable can not be converted to a
53 // constant union, since otherwise there's a chance that HLSL output will generate extra
Olli Etuahob5601eb2017-11-15 18:08:04 +020054 // statements from the initializer expression.
Olli Etuaho3d932d82016-04-12 11:10:30 +030055
Olli Etuahob5601eb2017-11-15 18:08:04 +020056 // Change const global to a regular global if its initialization is deferred.
57 // This can happen if ANGLE has not been able to fold the constant expression used
58 // as an initializer.
59 ASSERT(symbolNode->getQualifier() == EvqConst ||
60 symbolNode->getQualifier() == EvqGlobal);
61 if (symbolNode->getQualifier() == EvqConst)
Olli Etuahod4f4c112016-04-15 15:11:24 +030062 {
Olli Etuahob5601eb2017-11-15 18:08:04 +020063 symbolNode->getTypePointer()->setQualifier(EvqGlobal);
Olli Etuahod4f4c112016-04-15 15:11:24 +030064 }
Olli Etuahob5601eb2017-11-15 18:08:04 +020065
66 TIntermBinary *deferredInit =
67 new TIntermBinary(EOpAssign, symbolNode->deepCopy(), init->getRight());
68 deferredInitializersOut->push_back(deferredInit);
69
70 // Remove the initializer from the global scope and just declare the global instead.
71 declaration->replaceChildNode(init, symbolNode);
Olli Etuaho3d932d82016-04-12 11:10:30 +030072 }
Olli Etuahob5601eb2017-11-15 18:08:04 +020073 }
74 else if (initializeUninitializedGlobals)
75 {
76 TIntermSymbol *symbolNode = declarator->getAsSymbolNode();
77 ASSERT(symbolNode);
78
Olli Etuaho8b5e8fd2017-12-15 14:59:15 +020079 // Ignore ANGLE internal variables and nameless declarations.
80 if (symbolNode->variable().symbolType() == SymbolType::AngleInternal ||
81 symbolNode->variable().symbolType() == SymbolType::Empty)
Olli Etuahob5601eb2017-11-15 18:08:04 +020082 return;
83
Olli Etuaho8b5e8fd2017-12-15 14:59:15 +020084 if (symbolNode->getQualifier() == EvqGlobal)
Olli Etuaho0ffc4412017-05-19 14:18:55 +030085 {
Olli Etuaho87cc90d2017-12-12 15:28:06 +020086 TIntermSequence *initCode = CreateInitCode(symbolNode, canUseLoopsToInitialize,
87 highPrecisionSupported, symbolTable);
Olli Etuahob5601eb2017-11-15 18:08:04 +020088 deferredInitializersOut->insert(deferredInitializersOut->end(), initCode->begin(),
89 initCode->end());
Olli Etuaho0ffc4412017-05-19 14:18:55 +030090 }
Olli Etuaho3d932d82016-04-12 11:10:30 +030091 }
Olli Etuaho3d932d82016-04-12 11:10:30 +030092}
93
Olli Etuahoa16a84f2017-09-12 13:49:18 +030094void InsertInitCallToMain(TIntermBlock *root,
95 TIntermSequence *deferredInitializers,
96 TSymbolTable *symbolTable)
Olli Etuaho3d932d82016-04-12 11:10:30 +030097{
Olli Etuaho83498432017-05-19 14:57:23 +030098 TIntermBlock *initGlobalsBlock = new TIntermBlock();
99 initGlobalsBlock->getSequence()->swap(*deferredInitializers);
Olli Etuaho3d932d82016-04-12 11:10:30 +0300100
Olli Etuaho0c371002017-12-13 17:00:25 +0400101 TFunction *initGlobalsFunction =
102 new TFunction(symbolTable, NewPoolTString("initGlobals"), new TType(EbtVoid),
103 SymbolType::AngleInternal, false);
Olli Etuahoa16a84f2017-09-12 13:49:18 +0300104
105 TIntermFunctionPrototype *initGlobalsFunctionPrototype =
Olli Etuaho0c371002017-12-13 17:00:25 +0400106 CreateInternalFunctionPrototypeNode(*initGlobalsFunction);
Olli Etuahoa16a84f2017-09-12 13:49:18 +0300107 root->getSequence()->insert(root->getSequence()->begin(), initGlobalsFunctionPrototype);
Olli Etuaho0c371002017-12-13 17:00:25 +0400108 TIntermFunctionDefinition *initGlobalsFunctionDefinition =
109 CreateInternalFunctionDefinitionNode(*initGlobalsFunction, initGlobalsBlock);
Olli Etuahoa16a84f2017-09-12 13:49:18 +0300110 root->appendStatement(initGlobalsFunctionDefinition);
111
Olli Etuaho0c371002017-12-13 17:00:25 +0400112 TIntermAggregate *initGlobalsCall =
113 TIntermAggregate::CreateFunctionCall(*initGlobalsFunction, new TIntermSequence());
Olli Etuahoa16a84f2017-09-12 13:49:18 +0300114
Martin Radevb50ccd32017-07-06 17:09:58 +0300115 TIntermBlock *mainBody = FindMainBody(root);
Olli Etuahoa16a84f2017-09-12 13:49:18 +0300116 mainBody->getSequence()->insert(mainBody->getSequence()->begin(), initGlobalsCall);
Olli Etuaho3d932d82016-04-12 11:10:30 +0300117}
118
119} // namespace
120
Olli Etuahoa16a84f2017-09-12 13:49:18 +0300121void DeferGlobalInitializers(TIntermBlock *root,
122 bool initializeUninitializedGlobals,
Olli Etuaho2c7f34c2017-10-09 17:18:02 +0300123 bool canUseLoopsToInitialize,
Olli Etuaho87cc90d2017-12-12 15:28:06 +0200124 bool highPrecisionSupported,
Olli Etuahoa16a84f2017-09-12 13:49:18 +0300125 TSymbolTable *symbolTable)
Olli Etuaho3d932d82016-04-12 11:10:30 +0300126{
Olli Etuaho1dd07a62017-05-12 17:01:22 +0300127 TIntermSequence *deferredInitializers = new TIntermSequence();
Olli Etuaho3d932d82016-04-12 11:10:30 +0300128
Olli Etuaho1dd07a62017-05-12 17:01:22 +0300129 // Loop over all global statements and process the declarations. This is simpler than using a
130 // traverser.
Olli Etuaho0ffc4412017-05-19 14:18:55 +0300131 for (TIntermNode *statement : *root->getSequence())
Olli Etuaho1dd07a62017-05-12 17:01:22 +0300132 {
Olli Etuaho0ffc4412017-05-19 14:18:55 +0300133 TIntermDeclaration *declaration = statement->getAsDeclarationNode();
134 if (declaration)
Olli Etuaho1dd07a62017-05-12 17:01:22 +0300135 {
Olli Etuaho0ffc4412017-05-19 14:18:55 +0300136 GetDeferredInitializers(declaration, initializeUninitializedGlobals,
Olli Etuaho87cc90d2017-12-12 15:28:06 +0200137 canUseLoopsToInitialize, highPrecisionSupported,
138 deferredInitializers, symbolTable);
Olli Etuaho1dd07a62017-05-12 17:01:22 +0300139 }
140 }
Olli Etuaho3d932d82016-04-12 11:10:30 +0300141
142 // Add the function with initialization and the call to that.
Olli Etuaho1dd07a62017-05-12 17:01:22 +0300143 if (!deferredInitializers->empty())
144 {
Olli Etuahoa16a84f2017-09-12 13:49:18 +0300145 InsertInitCallToMain(root, deferredInitializers, symbolTable);
Olli Etuaho1dd07a62017-05-12 17:01:22 +0300146 }
Olli Etuaho3d932d82016-04-12 11:10:30 +0300147}
Jamie Madill45bcc782016-11-07 13:58:48 -0500148
149} // namespace sh