blob: ffb149ae72e4ba05a999485825ce6b9f0009cc2b [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//
6// DeferGlobalInitializers is an AST traverser that moves global initializers into a function, and
7// adds a function call to that function in the beginning of main().
8// This enables initialization of globals with uniforms or non-constant globals, as allowed by
9// the WebGL spec. Some initializers referencing non-constants may need to be unfolded into if
10// statements in HLSL - this kind of steps should be done after DeferGlobalInitializers is run.
11//
12
13#include "compiler/translator/DeferGlobalInitializers.h"
14
15#include "compiler/translator/IntermNode.h"
16#include "compiler/translator/SymbolTable.h"
17
Jamie Madill45bcc782016-11-07 13:58:48 -050018namespace sh
19{
20
Olli Etuaho3d932d82016-04-12 11:10:30 +030021namespace
22{
23
Olli Etuahobd674552016-10-06 13:28:42 +010024void SetInternalFunctionName(TFunctionSymbolInfo *functionInfo, const char *name)
Olli Etuaho3d932d82016-04-12 11:10:30 +030025{
26 TString nameStr(name);
27 nameStr = TFunction::mangleName(nameStr);
28 TName nameObj(nameStr);
29 nameObj.setInternal(true);
Olli Etuahobd674552016-10-06 13:28:42 +010030 functionInfo->setNameObj(nameObj);
Olli Etuaho3d932d82016-04-12 11:10:30 +030031}
32
Olli Etuaho16c745a2017-01-16 17:02:27 +000033TIntermFunctionPrototype *CreateFunctionPrototypeNode(const char *name, const int functionId)
Olli Etuaho3d932d82016-04-12 11:10:30 +030034{
Olli Etuaho16c745a2017-01-16 17:02:27 +000035 TType returnType(EbtVoid);
36 TIntermFunctionPrototype *functionNode = new TIntermFunctionPrototype(returnType);
Olli Etuaho3d932d82016-04-12 11:10:30 +030037
Olli Etuahobd674552016-10-06 13:28:42 +010038 SetInternalFunctionName(functionNode->getFunctionSymbolInfo(), name);
Olli Etuahobd674552016-10-06 13:28:42 +010039 functionNode->getFunctionSymbolInfo()->setId(functionId);
Olli Etuaho3d932d82016-04-12 11:10:30 +030040 return functionNode;
41}
42
Olli Etuaho336b1472016-10-05 16:37:55 +010043TIntermFunctionDefinition *CreateFunctionDefinitionNode(const char *name,
44 TIntermBlock *functionBody,
45 const int functionId)
Olli Etuaho3d932d82016-04-12 11:10:30 +030046{
Olli Etuaho8ad9e752017-01-16 19:55:20 +000047 TIntermFunctionPrototype *prototypeNode = CreateFunctionPrototypeNode(name, functionId);
48 return new TIntermFunctionDefinition(prototypeNode, functionBody);
Olli Etuaho3d932d82016-04-12 11:10:30 +030049}
50
Olli Etuahod4f4c112016-04-15 15:11:24 +030051TIntermAggregate *CreateFunctionCallNode(const char *name, const int functionId)
Olli Etuaho3d932d82016-04-12 11:10:30 +030052{
53 TIntermAggregate *functionNode = new TIntermAggregate(EOpFunctionCall);
54
Olli Etuahod4f4c112016-04-15 15:11:24 +030055 functionNode->setUserDefined();
Olli Etuahobd674552016-10-06 13:28:42 +010056 SetInternalFunctionName(functionNode->getFunctionSymbolInfo(), name);
Olli Etuaho3d932d82016-04-12 11:10:30 +030057 TType returnType(EbtVoid);
58 functionNode->setType(returnType);
Olli Etuahobd674552016-10-06 13:28:42 +010059 functionNode->getFunctionSymbolInfo()->setId(functionId);
Olli Etuaho3d932d82016-04-12 11:10:30 +030060 return functionNode;
61}
62
63class DeferGlobalInitializersTraverser : public TIntermTraverser
64{
65 public:
66 DeferGlobalInitializersTraverser();
67
68 bool visitBinary(Visit visit, TIntermBinary *node) override;
69
Olli Etuaho6d40bbd2016-09-30 13:49:38 +010070 void insertInitFunction(TIntermBlock *root);
Olli Etuaho3d932d82016-04-12 11:10:30 +030071
72 private:
73 TIntermSequence mDeferredInitializers;
74};
75
76DeferGlobalInitializersTraverser::DeferGlobalInitializersTraverser()
77 : TIntermTraverser(true, false, false)
78{
79}
80
81bool DeferGlobalInitializersTraverser::visitBinary(Visit visit, TIntermBinary *node)
82{
83 if (node->getOp() == EOpInitialize)
84 {
85 TIntermSymbol *symbolNode = node->getLeft()->getAsSymbolNode();
86 ASSERT(symbolNode);
87 TIntermTyped *expression = node->getRight();
88
Olli Etuahod4f4c112016-04-15 15:11:24 +030089 if (mInGlobalScope && (expression->getQualifier() != EvqConst ||
90 (expression->getAsConstantUnion() == nullptr &&
91 !expression->isConstructorWithOnlyConstantUnionParameters())))
Olli Etuaho3d932d82016-04-12 11:10:30 +030092 {
93 // For variables which are not constant, defer their real initialization until
94 // after we initialize uniforms.
95 // Deferral is done also in any cases where the variable has not been constant folded,
96 // since otherwise there's a chance that HLSL output will generate extra statements
97 // from the initializer expression.
Olli Etuaho3272a6d2016-08-29 17:54:50 +030098 TIntermBinary *deferredInit =
99 new TIntermBinary(EOpAssign, symbolNode->deepCopy(), node->getRight());
Olli Etuaho3d932d82016-04-12 11:10:30 +0300100 mDeferredInitializers.push_back(deferredInit);
101
Olli Etuahod4f4c112016-04-15 15:11:24 +0300102 // Change const global to a regular global if its initialization is deferred.
103 // This can happen if ANGLE has not been able to fold the constant expression used
104 // as an initializer.
105 ASSERT(symbolNode->getQualifier() == EvqConst ||
106 symbolNode->getQualifier() == EvqGlobal);
107 if (symbolNode->getQualifier() == EvqConst)
108 {
109 // All of the siblings in the same declaration need to have consistent qualifiers.
Olli Etuaho13389b62016-10-16 11:48:18 +0100110 auto *siblings = getParentNode()->getAsDeclarationNode()->getSequence();
Olli Etuahod4f4c112016-04-15 15:11:24 +0300111 for (TIntermNode *siblingNode : *siblings)
112 {
113 TIntermBinary *siblingBinary = siblingNode->getAsBinaryNode();
114 if (siblingBinary)
115 {
116 ASSERT(siblingBinary->getOp() == EOpInitialize);
117 siblingBinary->getLeft()->getTypePointer()->setQualifier(EvqGlobal);
118 }
119 siblingNode->getAsTyped()->getTypePointer()->setQualifier(EvqGlobal);
120 }
121 // This node is one of the siblings.
122 ASSERT(symbolNode->getQualifier() == EvqGlobal);
123 }
Olli Etuaho3d932d82016-04-12 11:10:30 +0300124 // Remove the initializer from the global scope and just declare the global instead.
Jamie Madill03d863c2016-07-27 18:15:53 -0400125 queueReplacement(node, symbolNode, OriginalNode::IS_DROPPED);
Olli Etuaho3d932d82016-04-12 11:10:30 +0300126 }
127 }
128 return false;
129}
130
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100131void DeferGlobalInitializersTraverser::insertInitFunction(TIntermBlock *root)
Olli Etuaho3d932d82016-04-12 11:10:30 +0300132{
133 if (mDeferredInitializers.empty())
134 {
135 return;
136 }
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500137 const int initFunctionId = TSymbolTable::nextUniqueId();
Olli Etuaho3d932d82016-04-12 11:10:30 +0300138
139 const char *functionName = "initializeDeferredGlobals";
140
141 // Add function prototype to the beginning of the shader
Olli Etuaho16c745a2017-01-16 17:02:27 +0000142 TIntermFunctionPrototype *functionPrototypeNode =
Olli Etuahod4f4c112016-04-15 15:11:24 +0300143 CreateFunctionPrototypeNode(functionName, initFunctionId);
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100144 root->getSequence()->insert(root->getSequence()->begin(), functionPrototypeNode);
Olli Etuaho3d932d82016-04-12 11:10:30 +0300145
146 // Add function definition to the end of the shader
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100147 TIntermBlock *functionBodyNode = new TIntermBlock();
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500148 TIntermSequence *functionBody = functionBodyNode->getSequence();
Olli Etuaho3d932d82016-04-12 11:10:30 +0300149 for (const auto &deferredInit : mDeferredInitializers)
150 {
151 functionBody->push_back(deferredInit);
152 }
Olli Etuaho336b1472016-10-05 16:37:55 +0100153 TIntermFunctionDefinition *functionDefinition =
Olli Etuahod4f4c112016-04-15 15:11:24 +0300154 CreateFunctionDefinitionNode(functionName, functionBodyNode, initFunctionId);
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100155 root->getSequence()->push_back(functionDefinition);
Olli Etuaho3d932d82016-04-12 11:10:30 +0300156
157 // Insert call into main function
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100158 for (TIntermNode *node : *root->getSequence())
Olli Etuaho3d932d82016-04-12 11:10:30 +0300159 {
Olli Etuaho336b1472016-10-05 16:37:55 +0100160 TIntermFunctionDefinition *nodeFunction = node->getAsFunctionDefinition();
161 if (nodeFunction != nullptr && nodeFunction->getFunctionSymbolInfo()->isMain())
Olli Etuaho3d932d82016-04-12 11:10:30 +0300162 {
Olli Etuahod4f4c112016-04-15 15:11:24 +0300163 TIntermAggregate *functionCallNode =
164 CreateFunctionCallNode(functionName, initFunctionId);
Olli Etuaho3d932d82016-04-12 11:10:30 +0300165
Olli Etuaho336b1472016-10-05 16:37:55 +0100166 TIntermBlock *mainBody = nodeFunction->getBody();
167 ASSERT(mainBody != nullptr);
168 mainBody->getSequence()->insert(mainBody->getSequence()->begin(), functionCallNode);
Olli Etuaho3d932d82016-04-12 11:10:30 +0300169 }
170 }
171}
172
173} // namespace
174
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100175void DeferGlobalInitializers(TIntermBlock *root)
Olli Etuaho3d932d82016-04-12 11:10:30 +0300176{
177 DeferGlobalInitializersTraverser traverser;
178 root->traverse(&traverser);
179
180 // Replace the initializers of the global variables.
181 traverser.updateTree();
182
183 // Add the function with initialization and the call to that.
184 traverser.insertInitFunction(root);
185}
Jamie Madill45bcc782016-11-07 13:58:48 -0500186
187} // namespace sh