blob: d55e12419aa2d3a1c57a49058ccb36f048e66a9f [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
Jamie Madill03d863c2016-07-27 18:15:53 -040078 queueReplacement(node, exp, OriginalNode::IS_DROPPED);
Olli Etuaho5c407bb2015-06-01 12:20:39 +030079
80 // If the x parameter also needs to be replaced, we need to do that in another traversal,
81 // since it's parent node will change in a way that's not handled correctly by updateTree().
82 if (IsProblematicPow(x))
83 {
84 mNeedAnotherIteration = true;
85 return false;
86 }
87 }
88 return true;
89}
90
91} // namespace
92
93void RemovePow(TIntermNode *root)
94{
95 RemovePowTraverser traverser;
96 // Iterate as necessary, and reset the traverser between iterations.
97 do
98 {
99 traverser.nextIteration();
100 root->traverse(&traverser);
101 traverser.updateTree();
102 }
103 while (traverser.needAnotherIteration());
104}