blob: 67d51ea87b9f7f995c90051179d6da5861967922 [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,
33 TIntermSequence *deferredInitializersOut,
34 TSymbolTable *symbolTable)
Olli Etuaho3d932d82016-04-12 11:10:30 +030035{
Olli Etuahob5601eb2017-11-15 18:08:04 +020036 // SeparateDeclarations should have already been run.
37 ASSERT(declaration->getSequence()->size() == 1);
38
39 TIntermNode *declarator = declaration->getSequence()->back();
40 TIntermBinary *init = declarator->getAsBinaryNode();
41 if (init)
Olli Etuaho3d932d82016-04-12 11:10:30 +030042 {
Olli Etuahob5601eb2017-11-15 18:08:04 +020043 TIntermSymbol *symbolNode = init->getLeft()->getAsSymbolNode();
44 ASSERT(symbolNode);
45 TIntermTyped *expression = init->getRight();
46
47 if ((expression->getQualifier() != EvqConst ||
48 (expression->getAsConstantUnion() == nullptr &&
49 !expression->isConstructorWithOnlyConstantUnionParameters())))
Olli Etuaho3d932d82016-04-12 11:10:30 +030050 {
Olli Etuahob5601eb2017-11-15 18:08:04 +020051 // For variables which are not constant, defer their real initialization until
52 // after we initialize uniforms.
53 // Deferral is done also in any cases where the variable has not been constant
54 // folded, since otherwise there's a chance that HLSL output will generate extra
55 // statements from the initializer expression.
Olli Etuaho3d932d82016-04-12 11:10:30 +030056
Olli Etuahob5601eb2017-11-15 18:08:04 +020057 // Change const global to a regular global if its initialization is deferred.
58 // This can happen if ANGLE has not been able to fold the constant expression used
59 // as an initializer.
60 ASSERT(symbolNode->getQualifier() == EvqConst ||
61 symbolNode->getQualifier() == EvqGlobal);
62 if (symbolNode->getQualifier() == EvqConst)
Olli Etuahod4f4c112016-04-15 15:11:24 +030063 {
Olli Etuahob5601eb2017-11-15 18:08:04 +020064 symbolNode->getTypePointer()->setQualifier(EvqGlobal);
Olli Etuahod4f4c112016-04-15 15:11:24 +030065 }
Olli Etuahob5601eb2017-11-15 18:08:04 +020066
67 TIntermBinary *deferredInit =
68 new TIntermBinary(EOpAssign, symbolNode->deepCopy(), init->getRight());
69 deferredInitializersOut->push_back(deferredInit);
70
71 // Remove the initializer from the global scope and just declare the global instead.
72 declaration->replaceChildNode(init, symbolNode);
Olli Etuaho3d932d82016-04-12 11:10:30 +030073 }
Olli Etuahob5601eb2017-11-15 18:08:04 +020074 }
75 else if (initializeUninitializedGlobals)
76 {
77 TIntermSymbol *symbolNode = declarator->getAsSymbolNode();
78 ASSERT(symbolNode);
79
80 // Ignore ANGLE internal variables.
81 if (symbolNode->getName().isInternal())
82 return;
83
84 if (symbolNode->getQualifier() == EvqGlobal && symbolNode->getSymbol() != "")
Olli Etuaho0ffc4412017-05-19 14:18:55 +030085 {
Olli Etuaho2c7f34c2017-10-09 17:18:02 +030086 TIntermSequence *initCode =
87 CreateInitCode(symbolNode, canUseLoopsToInitialize, 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 Etuahoa16a84f2017-09-12 13:49:18 +0300101 TSymbolUniqueId initGlobalsFunctionId(symbolTable);
102
103 const char *kInitGlobalsFunctionName = "initGlobals";
104
105 TIntermFunctionPrototype *initGlobalsFunctionPrototype =
106 CreateInternalFunctionPrototypeNode(TType(), kInitGlobalsFunctionName, initGlobalsFunctionId);
107 root->getSequence()->insert(root->getSequence()->begin(), initGlobalsFunctionPrototype);
108 TIntermFunctionDefinition *initGlobalsFunctionDefinition = CreateInternalFunctionDefinitionNode(
109 TType(), kInitGlobalsFunctionName, initGlobalsBlock, initGlobalsFunctionId);
110 root->appendStatement(initGlobalsFunctionDefinition);
111
112 TIntermAggregate *initGlobalsCall = CreateInternalFunctionCallNode(
113 TType(), kInitGlobalsFunctionName, initGlobalsFunctionId, new TIntermSequence());
114
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 Etuahoa16a84f2017-09-12 13:49:18 +0300124 TSymbolTable *symbolTable)
Olli Etuaho3d932d82016-04-12 11:10:30 +0300125{
Olli Etuaho1dd07a62017-05-12 17:01:22 +0300126 TIntermSequence *deferredInitializers = new TIntermSequence();
Olli Etuaho3d932d82016-04-12 11:10:30 +0300127
Olli Etuaho1dd07a62017-05-12 17:01:22 +0300128 // Loop over all global statements and process the declarations. This is simpler than using a
129 // traverser.
Olli Etuaho0ffc4412017-05-19 14:18:55 +0300130 for (TIntermNode *statement : *root->getSequence())
Olli Etuaho1dd07a62017-05-12 17:01:22 +0300131 {
Olli Etuaho0ffc4412017-05-19 14:18:55 +0300132 TIntermDeclaration *declaration = statement->getAsDeclarationNode();
133 if (declaration)
Olli Etuaho1dd07a62017-05-12 17:01:22 +0300134 {
Olli Etuaho0ffc4412017-05-19 14:18:55 +0300135 GetDeferredInitializers(declaration, initializeUninitializedGlobals,
Olli Etuaho2c7f34c2017-10-09 17:18:02 +0300136 canUseLoopsToInitialize, deferredInitializers, symbolTable);
Olli Etuaho1dd07a62017-05-12 17:01:22 +0300137 }
138 }
Olli Etuaho3d932d82016-04-12 11:10:30 +0300139
140 // Add the function with initialization and the call to that.
Olli Etuaho1dd07a62017-05-12 17:01:22 +0300141 if (!deferredInitializers->empty())
142 {
Olli Etuahoa16a84f2017-09-12 13:49:18 +0300143 InsertInitCallToMain(root, deferredInitializers, symbolTable);
Olli Etuaho1dd07a62017-05-12 17:01:22 +0300144 }
Olli Etuaho3d932d82016-04-12 11:10:30 +0300145}
Jamie Madill45bcc782016-11-07 13:58:48 -0500146
147} // namespace sh