summaryrefslogtreecommitdiffstats
path: root/src/3rdparty/angle/src/compiler/translator/BuiltInFunctionEmulator.cpp
blob: 51461207c5d4436ecf294495e8bc47bb807f5829 (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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
//
// Copyright (c) 2002-2011 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 "angle_gl.h"
#include "compiler/translator/BuiltInFunctionEmulator.h"
#include "compiler/translator/SymbolTable.h"

class BuiltInFunctionEmulator::BuiltInFunctionEmulationMarker : public TIntermTraverser
{
  public:
    BuiltInFunctionEmulationMarker(BuiltInFunctionEmulator& emulator)
        : mEmulator(emulator)
    {
    }

    virtual bool visitUnary(Visit visit, TIntermUnary* node)
    {
        if (visit == PreVisit) {
            bool needToEmulate = mEmulator.SetFunctionCalled(
                node->getOp(), node->getOperand()->getType());
            if (needToEmulate)
                node->setUseEmulatedFunction();
        }
        return true;
    }

    virtual bool visitAggregate(Visit visit, TIntermAggregate* node)
    {
        if (visit == PreVisit) {
            // Here we handle all the built-in functions instead of the ones we
            // currently identified as problematic.
            switch (node->getOp()) {
                case EOpLessThan:
                case EOpGreaterThan:
                case EOpLessThanEqual:
                case EOpGreaterThanEqual:
                case EOpVectorEqual:
                case EOpVectorNotEqual:
                case EOpMod:
                case EOpPow:
                case EOpAtan:
                case EOpMin:
                case EOpMax:
                case EOpClamp:
                case EOpMix:
                case EOpStep:
                case EOpSmoothStep:
                case EOpDistance:
                case EOpDot:
                case EOpCross:
                case EOpFaceForward:
                case EOpReflect:
                case EOpRefract:
                case EOpOuterProduct:
                case EOpMul:
                    break;
                default:
                    return true;
            };
            const TIntermSequence& sequence = *(node->getSequence());
            bool needToEmulate = false;
            // Right now we only handle built-in functions with two or three parameters.
            if (sequence.size() == 2)
            {
                TIntermTyped* param1 = sequence[0]->getAsTyped();
                TIntermTyped* param2 = sequence[1]->getAsTyped();
                if (!param1 || !param2)
                    return true;
                needToEmulate = mEmulator.SetFunctionCalled(
                    node->getOp(), param1->getType(), param2->getType());
            }
            else if (sequence.size() == 3)
            {
                TIntermTyped* param1 = sequence[0]->getAsTyped();
                TIntermTyped* param2 = sequence[1]->getAsTyped();
                TIntermTyped* param3 = sequence[2]->getAsTyped();
                if (!param1 || !param2 || !param3)
                    return true;
                needToEmulate = mEmulator.SetFunctionCalled(
                    node->getOp(), param1->getType(), param2->getType(), param3->getType());
            }
            else
            {
                return true;
            }

            if (needToEmulate)
                node->setUseEmulatedFunction();
        }
        return true;
    }

  private:
    BuiltInFunctionEmulator& mEmulator;
};

BuiltInFunctionEmulator::BuiltInFunctionEmulator()
{}

void BuiltInFunctionEmulator::addEmulatedFunction(
    TOperator op, const TType& param,
    const char* emulatedFunctionDefinition)
{
    mEmulatedFunctions[FunctionId(op, param)] =
        std::string(emulatedFunctionDefinition);
}

void BuiltInFunctionEmulator::addEmulatedFunction(
    TOperator op, const TType& param1, const TType& param2,
    const char* emulatedFunctionDefinition)
{
    mEmulatedFunctions[FunctionId(op, param1, param2)] =
        std::string(emulatedFunctionDefinition);
}

void BuiltInFunctionEmulator::addEmulatedFunction(
    TOperator op, const TType& param1, const TType& param2, const TType& param3,
    const char* emulatedFunctionDefinition)
{
    mEmulatedFunctions[FunctionId(op, param1, param2, param3)] =
        std::string(emulatedFunctionDefinition);
}

bool BuiltInFunctionEmulator::IsOutputEmpty() const
{
    return (mFunctions.size() == 0);
}

void BuiltInFunctionEmulator::OutputEmulatedFunctions(
    TInfoSinkBase& out) const
{
    for (size_t i = 0; i < mFunctions.size(); ++i) {
        out << mEmulatedFunctions.find(mFunctions[i])->second << "\n\n";
    }
}

bool BuiltInFunctionEmulator::SetFunctionCalled(
    TOperator op, const TType& param)
{
    return SetFunctionCalled(FunctionId(op, param));
}

bool BuiltInFunctionEmulator::SetFunctionCalled(
    TOperator op, const TType& param1, const TType& param2)
{
    return SetFunctionCalled(FunctionId(op, param1, param2));
}

bool BuiltInFunctionEmulator::SetFunctionCalled(
    TOperator op, const TType& param1, const TType& param2, const TType& param3)
{
    return SetFunctionCalled(FunctionId(op, param1, param2, param3));
}

bool BuiltInFunctionEmulator::SetFunctionCalled(
    const FunctionId& functionId) {
    if (mEmulatedFunctions.find(functionId) != mEmulatedFunctions.end())
    {
        for (size_t i = 0; i < mFunctions.size(); ++i) {
            if (mFunctions[i] == functionId)
                return true;
        }
        mFunctions.push_back(functionId);
        return true;
    }
    return false;
}

void BuiltInFunctionEmulator::MarkBuiltInFunctionsForEmulation(
    TIntermNode* root)
{
    ASSERT(root);

    if (mEmulatedFunctions.empty())
        return;

    BuiltInFunctionEmulationMarker marker(*this);
    root->traverse(&marker);
}

void BuiltInFunctionEmulator::Cleanup()
{
    mFunctions.clear();
}

//static
TString BuiltInFunctionEmulator::GetEmulatedFunctionName(
    const TString& name)
{
    ASSERT(name[name.length() - 1] == '(');
    return "webgl_" + name.substr(0, name.length() - 1) + "_emu(";
}

BuiltInFunctionEmulator::FunctionId::FunctionId
    (TOperator op, const TType& param)
    : mOp(op),
      mParam1(param),
      mParam2(EbtVoid),
      mParam3(EbtVoid)
{
}

BuiltInFunctionEmulator::FunctionId::FunctionId
    (TOperator op, const TType& param1, const TType& param2)
    : mOp(op),
      mParam1(param1),
      mParam2(param2),
      mParam3(EbtVoid)
{
}

BuiltInFunctionEmulator::FunctionId::FunctionId
    (TOperator op, const TType& param1, const TType& param2, const TType& param3)
    : mOp(op),
      mParam1(param1),
      mParam2(param2),
      mParam3(param3)
{
}

bool BuiltInFunctionEmulator::FunctionId::operator==
    (const BuiltInFunctionEmulator::FunctionId& other) const
{
    return (mOp == other.mOp &&
        mParam1 == other.mParam1 &&
        mParam2 == other.mParam2 &&
        mParam3 == other.mParam3);
}

bool BuiltInFunctionEmulator::FunctionId::operator<
    (const BuiltInFunctionEmulator::FunctionId& other) const
{
    if (mOp != other.mOp)
        return mOp < other.mOp;
    if (mParam1 != other.mParam1)
        return mParam1 < other.mParam1;
    if (mParam2 != other.mParam2)
        return mParam2 < other.mParam2;
    if (mParam3 != other.mParam3)
       return mParam3 < other.mParam3;
    return false; // all fields are equal
}