blob: 235f49893c411f0f5227362c245801bb0c2f9440 [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 {
48 case EOpSequence:
49 break;
50 case EOpFunction:
51 {
52 // Function definition.
53 ASSERT(visit == PreVisit);
54 if (node->getName() == "main(")
55 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -070056 TIntermSequence *sequence = node->getSequence();
Olli Etuahof51fdd22016-10-03 10:03:40 +010057 ASSERT(sequence->size() == 2);
58 TIntermAggregate *body = (*sequence)[1]->getAsAggregate();
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080059 ASSERT(body);
60 insertInitCode(body->getSequence());
61 mCodeInserted = true;
62 }
63 break;
64 }
65 default:
66 visitChildren = false;
67 break;
68 }
69 return visitChildren;
70}
71
Zhenyao Mo72111912016-07-20 17:45:56 -070072void VariableInitializer::insertInitCode(TIntermSequence *sequence)
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080073{
Corentin Wallez509e4562016-08-25 14:55:44 -040074 for (const auto &var : mVariables)
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080075 {
Zhenyao Mo72111912016-07-20 17:45:56 -070076 TString name = TString(var.name.c_str());
Corentin Wallez509e4562016-08-25 14:55:44 -040077 TType type = sh::GetShaderVariableType(var);
78
79 // Assign the array elements one by one to keep the AST compatible with ESSL 1.00 which
80 // doesn't have array assignment.
Zhenyao Mo72111912016-07-20 17:45:56 -070081 if (var.isArray())
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080082 {
Zhenyao Mo72111912016-07-20 17:45:56 -070083 size_t pos = name.find_last_of('[');
84 if (pos != TString::npos)
Corentin Wallez509e4562016-08-25 14:55:44 -040085 {
Zhenyao Mo72111912016-07-20 17:45:56 -070086 name = name.substr(0, pos);
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080087 }
Corentin Wallez509e4562016-08-25 14:55:44 -040088 TType elementType = type;
89 elementType.clearArrayness();
Zhenyao Mof9312682016-07-22 12:51:31 -070090
Corentin Wallez509e4562016-08-25 14:55:44 -040091 for (unsigned int i = 0; i < var.arraySize; ++i)
92 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +030093 TIntermSymbol *arraySymbol = new TIntermSymbol(0, name, type);
94 TIntermBinary *element = new TIntermBinary(EOpIndexDirect, arraySymbol,
95 TIntermTyped::CreateIndexNode(i));
Zhenyao Mof9312682016-07-22 12:51:31 -070096
Corentin Wallez509e4562016-08-25 14:55:44 -040097 TIntermTyped *zero = TIntermTyped::CreateZero(elementType);
98 TIntermBinary *assignment = new TIntermBinary(EOpAssign, element, zero);
99
100 sequence->insert(sequence->begin(), assignment);
Zhenyao Mof9312682016-07-22 12:51:31 -0700101 }
102 }
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800103 else
104 {
Zhenyao Mo72111912016-07-20 17:45:56 -0700105 TIntermSymbol *symbol = new TIntermSymbol(0, name, type);
Corentin Wallez509e4562016-08-25 14:55:44 -0400106 TIntermTyped *zero = TIntermTyped::CreateZero(type);
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800107
Corentin Wallez509e4562016-08-25 14:55:44 -0400108 TIntermBinary *assign = new TIntermBinary(EOpAssign, symbol, zero);
109 sequence->insert(sequence->begin(), assign);
110 }
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800111 }
112}
113
Zhenyao Mo72111912016-07-20 17:45:56 -0700114} // namespace anonymous
115
116void InitializeVariables(TIntermNode *root, const InitVariableList &vars)
117{
118 VariableInitializer initializer(vars);
119 root->traverse(&initializer);
120}