blob: 5161d51e7a6ccacd6dbab696aaa33b99bafce173 [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 Etuaho1dd07a62017-05-12 17:01:22 +030032 TIntermSequence *deferredInitializersOut)
Olli Etuaho3d932d82016-04-12 11:10:30 +030033{
Olli Etuahob5601eb2017-11-15 18:08:04 +020034 // SeparateDeclarations should have already been run.
35 ASSERT(declaration->getSequence()->size() == 1);
36
37 TIntermNode *declarator = declaration->getSequence()->back();
38 TIntermBinary *init = declarator->getAsBinaryNode();
39 if (init)
Olli Etuaho3d932d82016-04-12 11:10:30 +030040 {
Olli Etuahob5601eb2017-11-15 18:08:04 +020041 TIntermSymbol *symbolNode = init->getLeft()->getAsSymbolNode();
42 ASSERT(symbolNode);
43 TIntermTyped *expression = init->getRight();
44
45 if ((expression->getQualifier() != EvqConst ||
46 (expression->getAsConstantUnion() == nullptr &&
47 !expression->isConstructorWithOnlyConstantUnionParameters())))
Olli Etuaho3d932d82016-04-12 11:10:30 +030048 {
Olli Etuahob5601eb2017-11-15 18:08:04 +020049 // For variables which are not constant, defer their real initialization until
50 // after we initialize uniforms.
51 // Deferral is done also in any cases where the variable has not been constant
52 // folded, since otherwise there's a chance that HLSL output will generate extra
53 // statements from the initializer expression.
Olli Etuaho3d932d82016-04-12 11:10:30 +030054
Olli Etuahob5601eb2017-11-15 18:08:04 +020055 // Change const global to a regular global if its initialization is deferred.
56 // This can happen if ANGLE has not been able to fold the constant expression used
57 // as an initializer.
58 ASSERT(symbolNode->getQualifier() == EvqConst ||
59 symbolNode->getQualifier() == EvqGlobal);
60 if (symbolNode->getQualifier() == EvqConst)
Olli Etuahod4f4c112016-04-15 15:11:24 +030061 {
Olli Etuahob5601eb2017-11-15 18:08:04 +020062 symbolNode->getTypePointer()->setQualifier(EvqGlobal);
Olli Etuahod4f4c112016-04-15 15:11:24 +030063 }
Olli Etuahob5601eb2017-11-15 18:08:04 +020064
65 TIntermBinary *deferredInit =
66 new TIntermBinary(EOpAssign, symbolNode->deepCopy(), init->getRight());
67 deferredInitializersOut->push_back(deferredInit);
68
69 // Remove the initializer from the global scope and just declare the global instead.
70 declaration->replaceChildNode(init, symbolNode);
Olli Etuaho3d932d82016-04-12 11:10:30 +030071 }
Olli Etuahob5601eb2017-11-15 18:08:04 +020072 }
73 else if (initializeUninitializedGlobals)
74 {
75 TIntermSymbol *symbolNode = declarator->getAsSymbolNode();
76 ASSERT(symbolNode);
77
78 // Ignore ANGLE internal variables.
79 if (symbolNode->getName().isInternal())
80 return;
81
82 if (symbolNode->getQualifier() == EvqGlobal && symbolNode->getSymbol() != "")
Olli Etuaho0ffc4412017-05-19 14:18:55 +030083 {
Olli Etuahob5601eb2017-11-15 18:08:04 +020084 TIntermSequence *initCode = CreateInitCode(symbolNode);
85 deferredInitializersOut->insert(deferredInitializersOut->end(), initCode->begin(),
86 initCode->end());
Olli Etuaho0ffc4412017-05-19 14:18:55 +030087 }
Olli Etuaho3d932d82016-04-12 11:10:30 +030088 }
Olli Etuaho3d932d82016-04-12 11:10:30 +030089}
90
Olli Etuahoa16a84f2017-09-12 13:49:18 +030091void InsertInitCallToMain(TIntermBlock *root,
92 TIntermSequence *deferredInitializers,
93 TSymbolTable *symbolTable)
Olli Etuaho3d932d82016-04-12 11:10:30 +030094{
Olli Etuaho83498432017-05-19 14:57:23 +030095 TIntermBlock *initGlobalsBlock = new TIntermBlock();
96 initGlobalsBlock->getSequence()->swap(*deferredInitializers);
Olli Etuaho3d932d82016-04-12 11:10:30 +030097
Olli Etuahoa16a84f2017-09-12 13:49:18 +030098 TSymbolUniqueId initGlobalsFunctionId(symbolTable);
99
100 const char *kInitGlobalsFunctionName = "initGlobals";
101
102 TIntermFunctionPrototype *initGlobalsFunctionPrototype =
103 CreateInternalFunctionPrototypeNode(TType(), kInitGlobalsFunctionName, initGlobalsFunctionId);
104 root->getSequence()->insert(root->getSequence()->begin(), initGlobalsFunctionPrototype);
105 TIntermFunctionDefinition *initGlobalsFunctionDefinition = CreateInternalFunctionDefinitionNode(
106 TType(), kInitGlobalsFunctionName, initGlobalsBlock, initGlobalsFunctionId);
107 root->appendStatement(initGlobalsFunctionDefinition);
108
109 TIntermAggregate *initGlobalsCall = CreateInternalFunctionCallNode(
110 TType(), kInitGlobalsFunctionName, initGlobalsFunctionId, new TIntermSequence());
111
Martin Radevb50ccd32017-07-06 17:09:58 +0300112 TIntermBlock *mainBody = FindMainBody(root);
Olli Etuahoa16a84f2017-09-12 13:49:18 +0300113 mainBody->getSequence()->insert(mainBody->getSequence()->begin(), initGlobalsCall);
Olli Etuaho3d932d82016-04-12 11:10:30 +0300114}
115
116} // namespace
117
Olli Etuahoa16a84f2017-09-12 13:49:18 +0300118void DeferGlobalInitializers(TIntermBlock *root,
119 bool initializeUninitializedGlobals,
120 TSymbolTable *symbolTable)
Olli Etuaho3d932d82016-04-12 11:10:30 +0300121{
Olli Etuaho1dd07a62017-05-12 17:01:22 +0300122 TIntermSequence *deferredInitializers = new TIntermSequence();
Olli Etuaho3d932d82016-04-12 11:10:30 +0300123
Olli Etuaho1dd07a62017-05-12 17:01:22 +0300124 // Loop over all global statements and process the declarations. This is simpler than using a
125 // traverser.
Olli Etuaho0ffc4412017-05-19 14:18:55 +0300126 for (TIntermNode *statement : *root->getSequence())
Olli Etuaho1dd07a62017-05-12 17:01:22 +0300127 {
Olli Etuaho0ffc4412017-05-19 14:18:55 +0300128 TIntermDeclaration *declaration = statement->getAsDeclarationNode();
129 if (declaration)
Olli Etuaho1dd07a62017-05-12 17:01:22 +0300130 {
Olli Etuaho0ffc4412017-05-19 14:18:55 +0300131 GetDeferredInitializers(declaration, initializeUninitializedGlobals,
132 deferredInitializers);
Olli Etuaho1dd07a62017-05-12 17:01:22 +0300133 }
134 }
Olli Etuaho3d932d82016-04-12 11:10:30 +0300135
136 // Add the function with initialization and the call to that.
Olli Etuaho1dd07a62017-05-12 17:01:22 +0300137 if (!deferredInitializers->empty())
138 {
Olli Etuahoa16a84f2017-09-12 13:49:18 +0300139 InsertInitCallToMain(root, deferredInitializers, symbolTable);
Olli Etuaho1dd07a62017-05-12 17:01:22 +0300140 }
Olli Etuaho3d932d82016-04-12 11:10:30 +0300141}
Jamie Madill45bcc782016-11-07 13:58:48 -0500142
143} // namespace sh