blob: 48fc1f9aa2493a5293fcbcb4f86745eb1c2999ae [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 {
Olli Etuaho5c407bb2015-06-01 12:20:39 +030055 TIntermTyped *x = node->getSequence()->at(0)->getAsTyped();
56 TIntermTyped *y = node->getSequence()->at(1)->getAsTyped();
57
58 TIntermUnary *log = new TIntermUnary(EOpLog2);
59 log->setOperand(x);
60 log->setLine(node->getLine());
61 log->setType(x->getType());
62
Olli Etuaho3fdec912016-08-18 15:08:06 +030063 TIntermBinary *mul = new TIntermBinary(EOpMul, y, log);
Olli Etuaho5c407bb2015-06-01 12:20:39 +030064 mul->setLine(node->getLine());
Olli Etuaho3fdec912016-08-18 15:08:06 +030065 bool valid = mul->promote();
Olli Etuaho5c407bb2015-06-01 12:20:39 +030066 UNUSED_ASSERTION_VARIABLE(valid);
67 ASSERT(valid);
68
69 TIntermUnary *exp = new TIntermUnary(EOpExp2);
70 exp->setOperand(mul);
71 exp->setLine(node->getLine());
72 exp->setType(node->getType());
73
Jamie Madill03d863c2016-07-27 18:15:53 -040074 queueReplacement(node, exp, OriginalNode::IS_DROPPED);
Olli Etuaho5c407bb2015-06-01 12:20:39 +030075
76 // If the x parameter also needs to be replaced, we need to do that in another traversal,
77 // since it's parent node will change in a way that's not handled correctly by updateTree().
78 if (IsProblematicPow(x))
79 {
80 mNeedAnotherIteration = true;
81 return false;
82 }
83 }
84 return true;
85}
86
87} // namespace
88
89void RemovePow(TIntermNode *root)
90{
91 RemovePowTraverser traverser;
92 // Iterate as necessary, and reset the traverser between iterations.
93 do
94 {
95 traverser.nextIteration();
96 root->traverse(&traverser);
97 traverser.updateTree();
98 }
99 while (traverser.needAnotherIteration());
100}