summaryrefslogtreecommitdiffstats
path: root/gfx/angle/src/compiler/translator/InitializeVariables.cpp
blob: 21f00936eb8202f3ed4334540fc09ba65a76e630 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
//
// Copyright (c) 2002-2013 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//

#include "compiler/translator/InitializeVariables.h"

#include "angle_gl.h"
#include "common/debug.h"
#include "compiler/translator/IntermNode.h"
#include "compiler/translator/util.h"

namespace
{

TIntermConstantUnion *constructConstUnionNode(const TType &type)
{
    TType myType = type;
    myType.clearArrayness();
    myType.setQualifier(EvqConst);
    size_t size          = myType.getObjectSize();
    TConstantUnion *u = new TConstantUnion[size];
    for (size_t ii = 0; ii < size; ++ii)
    {
        switch (type.getBasicType())
        {
            case EbtFloat:
                u[ii].setFConst(0.0f);
                break;
            case EbtInt:
                u[ii].setIConst(0);
                break;
            case EbtUInt:
                u[ii].setUConst(0u);
                break;
            default:
                UNREACHABLE();
                return nullptr;
        }
    }

    TIntermConstantUnion *node = new TIntermConstantUnion(u, myType);
    return node;
}

TIntermConstantUnion *constructIndexNode(int index)
{
    TConstantUnion *u = new TConstantUnion[1];
    u[0].setIConst(index);

    TType type(EbtInt, EbpUndefined, EvqConst, 1);
    TIntermConstantUnion *node = new TIntermConstantUnion(u, type);
    return node;
}

class VariableInitializer : public TIntermTraverser
{
  public:
    VariableInitializer(const InitVariableList &vars)
        : TIntermTraverser(true, false, false), mVariables(vars), mCodeInserted(false)
    {
    }

  protected:
    bool visitBinary(Visit, TIntermBinary *node) override { return false; }
    bool visitUnary(Visit, TIntermUnary *node) override { return false; }
    bool visitSelection(Visit, TIntermSelection *node) override { return false; }
    bool visitLoop(Visit, TIntermLoop *node) override { return false; }
    bool visitBranch(Visit, TIntermBranch *node) override { return false; }

    bool visitAggregate(Visit visit, TIntermAggregate *node) override;

  private:
    void insertInitCode(TIntermSequence *sequence);

    const InitVariableList &mVariables;
    bool mCodeInserted;
};

// VariableInitializer implementation.

bool VariableInitializer::visitAggregate(Visit visit, TIntermAggregate *node)
{
    bool visitChildren = !mCodeInserted;
    switch (node->getOp())
    {
      case EOpSequence:
        break;
      case EOpFunction:
      {
        // Function definition.
        ASSERT(visit == PreVisit);
        if (node->getName() == "main(")
        {
            TIntermSequence *sequence = node->getSequence();
            ASSERT((sequence->size() == 1) || (sequence->size() == 2));
            TIntermAggregate *body = NULL;
            if (sequence->size() == 1)
            {
                body = new TIntermAggregate(EOpSequence);
                sequence->push_back(body);
            }
            else
            {
                body = (*sequence)[1]->getAsAggregate();
            }
            ASSERT(body);
            insertInitCode(body->getSequence());
            mCodeInserted = true;
        }
        break;
      }
      default:
        visitChildren = false;
        break;
    }
    return visitChildren;
}

void VariableInitializer::insertInitCode(TIntermSequence *sequence)
{
    for (size_t ii = 0; ii < mVariables.size(); ++ii)
    {
        const sh::ShaderVariable &var = mVariables[ii];
        TString name = TString(var.name.c_str());
        if (var.isArray())
        {
            TType type = sh::ConvertShaderVariableTypeToTType(var.type);
            size_t pos = name.find_last_of('[');
            if (pos != TString::npos)
                name = name.substr(0, pos);
            for (int index = static_cast<int>(var.arraySize) - 1; index >= 0; --index)
            {
                TIntermBinary *assign = new TIntermBinary(EOpAssign);
                sequence->insert(sequence->begin(), assign);

                TIntermBinary *indexDirect = new TIntermBinary(EOpIndexDirect);
                TIntermSymbol *symbol      = new TIntermSymbol(0, name, type);
                indexDirect->setLeft(symbol);
                TIntermConstantUnion *indexNode = constructIndexNode(index);
                indexDirect->setRight(indexNode);

                assign->setLeft(indexDirect);

                TIntermConstantUnion *zeroConst = constructConstUnionNode(type);
                assign->setRight(zeroConst);
            }
        }
        else if (var.isStruct())
        {
            TFieldList *fields = new TFieldList;
            TSourceLoc loc;
            for (auto field : var.fields)
            {
                fields->push_back(new TField(nullptr, new TString(field.name.c_str()), loc));
            }
            TStructure *structure = new TStructure(new TString(var.structName.c_str()), fields);
            TType type;
            type.setStruct(structure);
            for (int fieldIndex = 0; fieldIndex < static_cast<int>(var.fields.size()); ++fieldIndex)
            {
                TIntermBinary *assign = new TIntermBinary(EOpAssign);
                sequence->insert(sequence->begin(), assign);

                TIntermBinary *indexDirectStruct = new TIntermBinary(EOpIndexDirectStruct);
                TIntermSymbol *symbol            = new TIntermSymbol(0, name, type);
                indexDirectStruct->setLeft(symbol);
                TIntermConstantUnion *indexNode = constructIndexNode(fieldIndex);
                indexDirectStruct->setRight(indexNode);
                assign->setLeft(indexDirectStruct);

                const sh::ShaderVariable &field = var.fields[fieldIndex];
                TType fieldType                 = sh::ConvertShaderVariableTypeToTType(field.type);
                TIntermConstantUnion *zeroConst = constructConstUnionNode(fieldType);
                assign->setRight(zeroConst);
            }
        }
        else
        {
            TType type            = sh::ConvertShaderVariableTypeToTType(var.type);
            TIntermBinary *assign = new TIntermBinary(EOpAssign);
            sequence->insert(sequence->begin(), assign);
            TIntermSymbol *symbol = new TIntermSymbol(0, name, type);
            assign->setLeft(symbol);
            TIntermConstantUnion *zeroConst = constructConstUnionNode(type);
            assign->setRight(zeroConst);
        }

    }
}

}  // namespace anonymous

void InitializeVariables(TIntermNode *root, const InitVariableList &vars)
{
    VariableInitializer initializer(vars);
    root->traverse(&initializer);
}