Jamie Madill | 1048e43 | 2016-07-23 18:51:28 -0400 | [diff] [blame] | 1 | // |
| 2 | // Copyright (c) 2016 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 | // Implementation of the integer pow expressions HLSL bug workaround. |
| 7 | // See header for more info. |
| 8 | |
| 9 | #include "compiler/translator/ExpandIntegerPowExpressions.h" |
| 10 | |
| 11 | #include <cmath> |
| 12 | #include <cstdlib> |
| 13 | |
Olli Etuaho | c26214d | 2018-03-16 10:43:11 +0200 | [diff] [blame] | 14 | #include "compiler/translator/tree_util/IntermNode_util.h" |
| 15 | #include "compiler/translator/tree_util/IntermTraverse.h" |
Jamie Madill | 1048e43 | 2016-07-23 18:51:28 -0400 | [diff] [blame] | 16 | |
| 17 | namespace sh |
| 18 | { |
| 19 | |
| 20 | namespace |
| 21 | { |
| 22 | |
| 23 | class Traverser : public TIntermTraverser |
| 24 | { |
| 25 | public: |
Olli Etuaho | a5e693a | 2017-07-13 16:07:26 +0300 | [diff] [blame] | 26 | static void Apply(TIntermNode *root, TSymbolTable *symbolTable); |
Jamie Madill | 1048e43 | 2016-07-23 18:51:28 -0400 | [diff] [blame] | 27 | |
| 28 | private: |
Olli Etuaho | a5e693a | 2017-07-13 16:07:26 +0300 | [diff] [blame] | 29 | Traverser(TSymbolTable *symbolTable); |
Jamie Madill | 1048e43 | 2016-07-23 18:51:28 -0400 | [diff] [blame] | 30 | bool visitAggregate(Visit visit, TIntermAggregate *node) override; |
Jamie Madill | 5655b84 | 2016-08-02 11:00:07 -0400 | [diff] [blame] | 31 | void nextIteration(); |
| 32 | |
| 33 | bool mFound = false; |
Jamie Madill | 1048e43 | 2016-07-23 18:51:28 -0400 | [diff] [blame] | 34 | }; |
| 35 | |
| 36 | // static |
Olli Etuaho | a5e693a | 2017-07-13 16:07:26 +0300 | [diff] [blame] | 37 | void Traverser::Apply(TIntermNode *root, TSymbolTable *symbolTable) |
Jamie Madill | 1048e43 | 2016-07-23 18:51:28 -0400 | [diff] [blame] | 38 | { |
Olli Etuaho | a5e693a | 2017-07-13 16:07:26 +0300 | [diff] [blame] | 39 | Traverser traverser(symbolTable); |
Jamie Madill | 5655b84 | 2016-08-02 11:00:07 -0400 | [diff] [blame] | 40 | do |
| 41 | { |
| 42 | traverser.nextIteration(); |
| 43 | root->traverse(&traverser); |
| 44 | if (traverser.mFound) |
| 45 | { |
| 46 | traverser.updateTree(); |
| 47 | } |
| 48 | } while (traverser.mFound); |
Jamie Madill | 1048e43 | 2016-07-23 18:51:28 -0400 | [diff] [blame] | 49 | } |
| 50 | |
Olli Etuaho | a5e693a | 2017-07-13 16:07:26 +0300 | [diff] [blame] | 51 | Traverser::Traverser(TSymbolTable *symbolTable) : TIntermTraverser(true, false, false, symbolTable) |
Jamie Madill | 1048e43 | 2016-07-23 18:51:28 -0400 | [diff] [blame] | 52 | { |
| 53 | } |
| 54 | |
Jamie Madill | 5655b84 | 2016-08-02 11:00:07 -0400 | [diff] [blame] | 55 | void Traverser::nextIteration() |
| 56 | { |
| 57 | mFound = false; |
Jamie Madill | 5655b84 | 2016-08-02 11:00:07 -0400 | [diff] [blame] | 58 | } |
| 59 | |
Jamie Madill | 1048e43 | 2016-07-23 18:51:28 -0400 | [diff] [blame] | 60 | bool Traverser::visitAggregate(Visit visit, TIntermAggregate *node) |
| 61 | { |
Jamie Madill | 5655b84 | 2016-08-02 11:00:07 -0400 | [diff] [blame] | 62 | if (mFound) |
| 63 | { |
| 64 | return false; |
| 65 | } |
| 66 | |
Jamie Madill | 1048e43 | 2016-07-23 18:51:28 -0400 | [diff] [blame] | 67 | // Test 0: skip non-pow operators. |
| 68 | if (node->getOp() != EOpPow) |
| 69 | { |
| 70 | return true; |
| 71 | } |
| 72 | |
| 73 | const TIntermSequence *sequence = node->getSequence(); |
| 74 | ASSERT(sequence->size() == 2u); |
Olli Etuaho | 629a644 | 2017-12-11 10:55:43 +0200 | [diff] [blame] | 75 | const TIntermConstantUnion *constantExponent = sequence->at(1)->getAsConstantUnion(); |
Jamie Madill | 1048e43 | 2016-07-23 18:51:28 -0400 | [diff] [blame] | 76 | |
| 77 | // Test 1: check for a single constant. |
Olli Etuaho | 629a644 | 2017-12-11 10:55:43 +0200 | [diff] [blame] | 78 | if (!constantExponent || constantExponent->getNominalSize() != 1) |
Jamie Madill | 1048e43 | 2016-07-23 18:51:28 -0400 | [diff] [blame] | 79 | { |
| 80 | return true; |
| 81 | } |
| 82 | |
Olli Etuaho | 629a644 | 2017-12-11 10:55:43 +0200 | [diff] [blame] | 83 | ASSERT(constantExponent->getBasicType() == EbtFloat); |
Olli Etuaho | ea22b7a | 2018-01-04 17:09:11 +0200 | [diff] [blame] | 84 | float exponentValue = constantExponent->getConstantValue()->getFConst(); |
Jamie Madill | 1048e43 | 2016-07-23 18:51:28 -0400 | [diff] [blame] | 85 | |
Olli Etuaho | 629a644 | 2017-12-11 10:55:43 +0200 | [diff] [blame] | 86 | // Test 2: exponentValue is in the problematic range. |
| 87 | if (exponentValue < -5.0f || exponentValue > 9.0f) |
Jamie Madill | 1048e43 | 2016-07-23 18:51:28 -0400 | [diff] [blame] | 88 | { |
| 89 | return true; |
| 90 | } |
| 91 | |
Olli Etuaho | 629a644 | 2017-12-11 10:55:43 +0200 | [diff] [blame] | 92 | // Test 3: exponentValue is integer or pretty close to an integer. |
| 93 | if (std::abs(exponentValue - std::round(exponentValue)) > 0.0001f) |
Jamie Madill | 1048e43 | 2016-07-23 18:51:28 -0400 | [diff] [blame] | 94 | { |
| 95 | return true; |
| 96 | } |
| 97 | |
| 98 | // Test 4: skip -1, 0, and 1 |
Olli Etuaho | 629a644 | 2017-12-11 10:55:43 +0200 | [diff] [blame] | 99 | int exponent = static_cast<int>(std::round(exponentValue)); |
Jamie Madill | 1048e43 | 2016-07-23 18:51:28 -0400 | [diff] [blame] | 100 | int n = std::abs(exponent); |
| 101 | if (n < 2) |
| 102 | { |
| 103 | return true; |
| 104 | } |
| 105 | |
| 106 | // Potential problem case detected, apply workaround. |
Jamie Madill | 1048e43 | 2016-07-23 18:51:28 -0400 | [diff] [blame] | 107 | |
| 108 | TIntermTyped *lhs = sequence->at(0)->getAsTyped(); |
| 109 | ASSERT(lhs); |
| 110 | |
Olli Etuaho | 195be94 | 2017-12-04 23:40:14 +0200 | [diff] [blame] | 111 | TIntermDeclaration *lhsVariableDeclaration = nullptr; |
| 112 | TVariable *lhsVariable = |
| 113 | DeclareTempVariable(mSymbolTable, lhs, EvqTemporary, &lhsVariableDeclaration); |
| 114 | insertStatementInParentBlock(lhsVariableDeclaration); |
Jamie Madill | 1048e43 | 2016-07-23 18:51:28 -0400 | [diff] [blame] | 115 | |
| 116 | // Create a chain of n-1 multiples. |
Olli Etuaho | 195be94 | 2017-12-04 23:40:14 +0200 | [diff] [blame] | 117 | TIntermTyped *current = CreateTempSymbolNode(lhsVariable); |
Jamie Madill | 1048e43 | 2016-07-23 18:51:28 -0400 | [diff] [blame] | 118 | for (int i = 1; i < n; ++i) |
| 119 | { |
Olli Etuaho | 195be94 | 2017-12-04 23:40:14 +0200 | [diff] [blame] | 120 | TIntermBinary *mul = new TIntermBinary(EOpMul, current, CreateTempSymbolNode(lhsVariable)); |
Jamie Madill | 1048e43 | 2016-07-23 18:51:28 -0400 | [diff] [blame] | 121 | mul->setLine(node->getLine()); |
| 122 | current = mul; |
| 123 | } |
| 124 | |
| 125 | // For negative pow, compute the reciprocal of the positive pow. |
| 126 | if (exponent < 0) |
| 127 | { |
| 128 | TConstantUnion *oneVal = new TConstantUnion(); |
| 129 | oneVal->setFConst(1.0f); |
| 130 | TIntermConstantUnion *oneNode = new TIntermConstantUnion(oneVal, node->getType()); |
Olli Etuaho | 3272a6d | 2016-08-29 17:54:50 +0300 | [diff] [blame] | 131 | TIntermBinary *div = new TIntermBinary(EOpDiv, oneNode, current); |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 132 | current = div; |
Jamie Madill | 1048e43 | 2016-07-23 18:51:28 -0400 | [diff] [blame] | 133 | } |
| 134 | |
Olli Etuaho | ea39a22 | 2017-07-06 12:47:59 +0300 | [diff] [blame] | 135 | queueReplacement(current, OriginalNode::IS_DROPPED); |
Jamie Madill | 5655b84 | 2016-08-02 11:00:07 -0400 | [diff] [blame] | 136 | mFound = true; |
| 137 | return false; |
Jamie Madill | 1048e43 | 2016-07-23 18:51:28 -0400 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | } // anonymous namespace |
| 141 | |
Olli Etuaho | a5e693a | 2017-07-13 16:07:26 +0300 | [diff] [blame] | 142 | void ExpandIntegerPowExpressions(TIntermNode *root, TSymbolTable *symbolTable) |
Jamie Madill | 1048e43 | 2016-07-23 18:51:28 -0400 | [diff] [blame] | 143 | { |
Olli Etuaho | a5e693a | 2017-07-13 16:07:26 +0300 | [diff] [blame] | 144 | Traverser::Apply(root, symbolTable); |
Jamie Madill | 1048e43 | 2016-07-23 18:51:28 -0400 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | } // namespace sh |