blob: d44f2fa0a3f3cabca065fd8235054bf5c91ddadb [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; }
31
32 bool visitAggregate(Visit visit, TIntermAggregate *node) override;
33
34 private:
35 void insertInitCode(TIntermSequence *sequence);
36
37 const InitVariableList &mVariables;
38 bool mCodeInserted;
39};
40
41// VariableInitializer implementation.
42
43bool VariableInitializer::visitAggregate(Visit visit, TIntermAggregate *node)
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080044{
45 bool visitChildren = !mCodeInserted;
46 switch (node->getOp())
47 {
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080048 case EOpFunction:
49 {
50 // Function definition.
51 ASSERT(visit == PreVisit);
52 if (node->getName() == "main(")
53 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -070054 TIntermSequence *sequence = node->getSequence();
Olli Etuahof51fdd22016-10-03 10:03:40 +010055 ASSERT(sequence->size() == 2);
Olli Etuaho6d40bbd2016-09-30 13:49:38 +010056 TIntermBlock *body = (*sequence)[1]->getAsBlock();
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080057 ASSERT(body);
58 insertInitCode(body->getSequence());
59 mCodeInserted = true;
60 }
61 break;
62 }
63 default:
64 visitChildren = false;
65 break;
66 }
67 return visitChildren;
68}
69
Zhenyao Mo72111912016-07-20 17:45:56 -070070void VariableInitializer::insertInitCode(TIntermSequence *sequence)
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080071{
Corentin Wallez509e4562016-08-25 14:55:44 -040072 for (const auto &var : mVariables)
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080073 {
Zhenyao Mo72111912016-07-20 17:45:56 -070074 TString name = TString(var.name.c_str());
Corentin Wallez509e4562016-08-25 14:55:44 -040075 TType type = sh::GetShaderVariableType(var);
76
77 // Assign the array elements one by one to keep the AST compatible with ESSL 1.00 which
78 // doesn't have array assignment.
Zhenyao Mo72111912016-07-20 17:45:56 -070079 if (var.isArray())
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080080 {
Zhenyao Mo72111912016-07-20 17:45:56 -070081 size_t pos = name.find_last_of('[');
82 if (pos != TString::npos)
Corentin Wallez509e4562016-08-25 14:55:44 -040083 {
Zhenyao Mo72111912016-07-20 17:45:56 -070084 name = name.substr(0, pos);
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080085 }
Corentin Wallez509e4562016-08-25 14:55:44 -040086 TType elementType = type;
87 elementType.clearArrayness();
Zhenyao Mof9312682016-07-22 12:51:31 -070088
Corentin Wallez509e4562016-08-25 14:55:44 -040089 for (unsigned int i = 0; i < var.arraySize; ++i)
90 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +030091 TIntermSymbol *arraySymbol = new TIntermSymbol(0, name, type);
92 TIntermBinary *element = new TIntermBinary(EOpIndexDirect, arraySymbol,
93 TIntermTyped::CreateIndexNode(i));
Zhenyao Mof9312682016-07-22 12:51:31 -070094
Corentin Wallez509e4562016-08-25 14:55:44 -040095 TIntermTyped *zero = TIntermTyped::CreateZero(elementType);
96 TIntermBinary *assignment = new TIntermBinary(EOpAssign, element, zero);
97
98 sequence->insert(sequence->begin(), assignment);
Zhenyao Mof9312682016-07-22 12:51:31 -070099 }
100 }
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800101 else
102 {
Zhenyao Mo72111912016-07-20 17:45:56 -0700103 TIntermSymbol *symbol = new TIntermSymbol(0, name, type);
Corentin Wallez509e4562016-08-25 14:55:44 -0400104 TIntermTyped *zero = TIntermTyped::CreateZero(type);
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800105
Corentin Wallez509e4562016-08-25 14:55:44 -0400106 TIntermBinary *assign = new TIntermBinary(EOpAssign, symbol, zero);
107 sequence->insert(sequence->begin(), assign);
108 }
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800109 }
110}
111
Zhenyao Mo72111912016-07-20 17:45:56 -0700112} // namespace anonymous
113
114void InitializeVariables(TIntermNode *root, const InitVariableList &vars)
115{
116 VariableInitializer initializer(vars);
117 root->traverse(&initializer);
118}