blob: 6dbb48da9cde411bc733109cdd68f16650273015 [file] [log] [blame]
Olli Etuaho5c407bb2015-06-01 12:20:39 +03001//
2// Copyright (c) 2002-2015 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// RemovePow is an AST traverser to convert pow(x, y) built-in calls where y is a
7// constant to exp2(y * log2(x)). This works around an issue in NVIDIA 311 series
8// OpenGL drivers.
9//
10
11#include "compiler/translator/RemovePow.h"
12
13#include "compiler/translator/InfoSink.h"
14#include "compiler/translator/IntermNode.h"
15
16namespace
17{
18
19bool IsProblematicPow(TIntermTyped *node)
20{
21 TIntermAggregate *agg = node->getAsAggregate();
22 if (agg != nullptr && agg->getOp() == EOpPow)
23 {
24 ASSERT(agg->getSequence()->size() == 2);
25 return agg->getSequence()->at(1)->getAsConstantUnion() != nullptr;
26 }
27 return false;
28}
29
30// Traverser that converts all pow operations simultaneously.
31class RemovePowTraverser : public TIntermTraverser
32{
33 public:
34 RemovePowTraverser();
35
36 bool visitAggregate(Visit visit, TIntermAggregate *node) override;
37
38 void nextIteration() { mNeedAnotherIteration = false; }
39 bool needAnotherIteration() const { return mNeedAnotherIteration; }
40
41 protected:
42 bool mNeedAnotherIteration;
43};
44
45RemovePowTraverser::RemovePowTraverser()
46 : TIntermTraverser(true, false, false),
47 mNeedAnotherIteration(false)
48{
49}
50
51bool RemovePowTraverser::visitAggregate(Visit visit, TIntermAggregate *node)
52{
53 if (IsProblematicPow(node))
54 {
55 TInfoSink nullSink;
56
57 TIntermTyped *x = node->getSequence()->at(0)->getAsTyped();
58 TIntermTyped *y = node->getSequence()->at(1)->getAsTyped();
59
60 TIntermUnary *log = new TIntermUnary(EOpLog2);
61 log->setOperand(x);
62 log->setLine(node->getLine());
63 log->setType(x->getType());
64
65 TIntermBinary *mul = new TIntermBinary(EOpMul);
66 mul->setLeft(y);
67 mul->setRight(log);
68 mul->setLine(node->getLine());
69 bool valid = mul->promote(nullSink);
70 UNUSED_ASSERTION_VARIABLE(valid);
71 ASSERT(valid);
72
73 TIntermUnary *exp = new TIntermUnary(EOpExp2);
74 exp->setOperand(mul);
75 exp->setLine(node->getLine());
76 exp->setType(node->getType());
77
78 NodeUpdateEntry replacePow(getParentNode(), node, exp, false);
79 mReplacements.push_back(replacePow);
80
81 // If the x parameter also needs to be replaced, we need to do that in another traversal,
82 // since it's parent node will change in a way that's not handled correctly by updateTree().
83 if (IsProblematicPow(x))
84 {
85 mNeedAnotherIteration = true;
86 return false;
87 }
88 }
89 return true;
90}
91
92} // namespace
93
94void RemovePow(TIntermNode *root)
95{
96 RemovePowTraverser traverser;
97 // Iterate as necessary, and reset the traverser between iterations.
98 do
99 {
100 traverser.nextIteration();
101 root->traverse(&traverser);
102 traverser.updateTree();
103 }
104 while (traverser.needAnotherIteration());
105}