blob: 2deff0c5ef1f75c2d7d8231c27cd63311ebb720e [file] [log] [blame]
Zhenyao Mo4a667fe2014-02-11 12:35:01 -08001//
2// Copyright (c) 2002-2013 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
7#include "compiler/translator/InitializeVariables.h"
Olli Etuahod57e0db2015-04-24 15:05:08 +03008
Zhenyao Mo72111912016-07-20 17:45:56 -07009#include "angle_gl.h"
Olli Etuahod57e0db2015-04-24 15:05:08 +030010#include "common/debug.h"
Zhenyao Mo72111912016-07-20 17:45:56 -070011#include "compiler/translator/IntermNode.h"
12#include "compiler/translator/util.h"
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080013
14namespace
15{
16
Zhenyao Mo72111912016-07-20 17:45:56 -070017class VariableInitializer : public TIntermTraverser
18{
19 public:
20 VariableInitializer(const InitVariableList &vars)
21 : TIntermTraverser(true, false, false), mVariables(vars), mCodeInserted(false)
22 {
23 }
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080024
Zhenyao Mo72111912016-07-20 17:45:56 -070025 protected:
26 bool visitBinary(Visit, TIntermBinary *node) override { return false; }
27 bool visitUnary(Visit, TIntermUnary *node) override { return false; }
Olli Etuaho57961272016-09-14 13:57:46 +030028 bool visitIfElse(Visit, TIntermIfElse *node) override { return false; }
Zhenyao Mo72111912016-07-20 17:45:56 -070029 bool visitLoop(Visit, TIntermLoop *node) override { return false; }
30 bool visitBranch(Visit, TIntermBranch *node) override { return false; }
Olli Etuaho336b1472016-10-05 16:37:55 +010031 bool visitAggregate(Visit, TIntermAggregate *node) override { return false; }
Zhenyao Mo72111912016-07-20 17:45:56 -070032
Olli Etuaho336b1472016-10-05 16:37:55 +010033 bool visitFunctionDefinition(Visit visit, TIntermFunctionDefinition *node) override;
Zhenyao Mo72111912016-07-20 17:45:56 -070034
35 private:
36 void insertInitCode(TIntermSequence *sequence);
37
38 const InitVariableList &mVariables;
39 bool mCodeInserted;
40};
41
42// VariableInitializer implementation.
43
Olli Etuaho336b1472016-10-05 16:37:55 +010044bool VariableInitializer::visitFunctionDefinition(Visit visit, TIntermFunctionDefinition *node)
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080045{
Olli Etuaho336b1472016-10-05 16:37:55 +010046 // Function definition.
47 ASSERT(visit == PreVisit);
48 if (node->getFunctionSymbolInfo()->isMain())
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080049 {
Olli Etuaho336b1472016-10-05 16:37:55 +010050 TIntermBlock *body = node->getBody();
51 insertInitCode(body->getSequence());
52 mCodeInserted = true;
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080053 }
Olli Etuaho336b1472016-10-05 16:37:55 +010054 return false;
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080055}
56
Zhenyao Mo72111912016-07-20 17:45:56 -070057void VariableInitializer::insertInitCode(TIntermSequence *sequence)
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080058{
Corentin Wallez509e4562016-08-25 14:55:44 -040059 for (const auto &var : mVariables)
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080060 {
Zhenyao Mo72111912016-07-20 17:45:56 -070061 TString name = TString(var.name.c_str());
Corentin Wallez509e4562016-08-25 14:55:44 -040062 TType type = sh::GetShaderVariableType(var);
63
64 // Assign the array elements one by one to keep the AST compatible with ESSL 1.00 which
65 // doesn't have array assignment.
Zhenyao Mo72111912016-07-20 17:45:56 -070066 if (var.isArray())
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080067 {
Zhenyao Mo72111912016-07-20 17:45:56 -070068 size_t pos = name.find_last_of('[');
69 if (pos != TString::npos)
Corentin Wallez509e4562016-08-25 14:55:44 -040070 {
Zhenyao Mo72111912016-07-20 17:45:56 -070071 name = name.substr(0, pos);
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080072 }
Corentin Wallez509e4562016-08-25 14:55:44 -040073 TType elementType = type;
74 elementType.clearArrayness();
Zhenyao Mof9312682016-07-22 12:51:31 -070075
Corentin Wallez509e4562016-08-25 14:55:44 -040076 for (unsigned int i = 0; i < var.arraySize; ++i)
77 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +030078 TIntermSymbol *arraySymbol = new TIntermSymbol(0, name, type);
79 TIntermBinary *element = new TIntermBinary(EOpIndexDirect, arraySymbol,
80 TIntermTyped::CreateIndexNode(i));
Zhenyao Mof9312682016-07-22 12:51:31 -070081
Corentin Wallez509e4562016-08-25 14:55:44 -040082 TIntermTyped *zero = TIntermTyped::CreateZero(elementType);
83 TIntermBinary *assignment = new TIntermBinary(EOpAssign, element, zero);
84
85 sequence->insert(sequence->begin(), assignment);
Zhenyao Mof9312682016-07-22 12:51:31 -070086 }
87 }
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080088 else
89 {
Zhenyao Mo72111912016-07-20 17:45:56 -070090 TIntermSymbol *symbol = new TIntermSymbol(0, name, type);
Corentin Wallez509e4562016-08-25 14:55:44 -040091 TIntermTyped *zero = TIntermTyped::CreateZero(type);
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080092
Corentin Wallez509e4562016-08-25 14:55:44 -040093 TIntermBinary *assign = new TIntermBinary(EOpAssign, symbol, zero);
94 sequence->insert(sequence->begin(), assign);
95 }
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080096 }
97}
98
Zhenyao Mo72111912016-07-20 17:45:56 -070099} // namespace anonymous
100
101void InitializeVariables(TIntermNode *root, const InitVariableList &vars)
102{
103 VariableInitializer initializer(vars);
104 root->traverse(&initializer);
105}