1 //
2 // Copyright 2002 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 // During parsing, all constant expressions are folded to constant union nodes. The expressions that
7 // have been folded may have had precision qualifiers, which should affect the precision of the
8 // consuming operation. If the folded constant union nodes are written to output as such they won't
9 // have any precision qualifiers, and their effect on the precision of the consuming operation is
10 // lost.
11 //
12 // RecordConstantPrecision is an AST traverser that inspects the precision qualifiers of constants
13 // and hoists the constants outside the containing expression as precision qualified named variables
14 // in case that is required for correct precision propagation.
15 //
16
17 #include "compiler/translator/tree_ops/RecordConstantPrecision.h"
18
19 #include "compiler/translator/InfoSink.h"
20 #include "compiler/translator/tree_util/IntermNode_util.h"
21 #include "compiler/translator/tree_util/IntermTraverse.h"
22
23 namespace sh
24 {
25
26 namespace
27 {
28
29 class RecordConstantPrecisionTraverser : public TIntermTraverser
30 {
31 public:
32 RecordConstantPrecisionTraverser(TSymbolTable *symbolTable);
33
34 void visitConstantUnion(TIntermConstantUnion *node) override;
35
36 protected:
37 bool operandAffectsParentOperationPrecision(TIntermTyped *operand);
38 };
39
RecordConstantPrecisionTraverser(TSymbolTable * symbolTable)40 RecordConstantPrecisionTraverser::RecordConstantPrecisionTraverser(TSymbolTable *symbolTable)
41 : TIntermTraverser(true, false, true, symbolTable)
42 {}
43
operandAffectsParentOperationPrecision(TIntermTyped * operand)44 bool RecordConstantPrecisionTraverser::operandAffectsParentOperationPrecision(TIntermTyped *operand)
45 {
46 if (getParentNode()->getAsCaseNode() || getParentNode()->getAsBlock())
47 {
48 return false;
49 }
50
51 if (operand->getBasicType() == EbtBool || operand->getBasicType() == EbtStruct)
52 {
53 return false;
54 }
55
56 const TIntermBinary *parentAsBinary = getParentNode()->getAsBinaryNode();
57 if (parentAsBinary != nullptr)
58 {
59 // If the constant is assigned or is used to initialize a variable, or if it's an index,
60 // its precision has no effect.
61 switch (parentAsBinary->getOp())
62 {
63 case EOpInitialize:
64 case EOpAssign:
65 case EOpIndexDirect:
66 case EOpIndexDirectStruct:
67 case EOpIndexDirectInterfaceBlock:
68 case EOpIndexIndirect:
69 return false;
70 default:
71 return true;
72 }
73 }
74
75 TIntermAggregate *parentAsAggregate = getParentNode()->getAsAggregate();
76 if (parentAsAggregate != nullptr)
77 {
78 // The precision of an aggregate is derived from children only in the following conditions:
79 //
80 // - Built-in math operations
81 // - Constructors
82 //
83 return parentAsAggregate->isConstructor() ||
84 BuiltInGroup::IsMath(parentAsAggregate->getOp());
85 }
86
87 return true;
88 }
89
visitConstantUnion(TIntermConstantUnion * node)90 void RecordConstantPrecisionTraverser::visitConstantUnion(TIntermConstantUnion *node)
91 {
92 // If the constant has lowp or undefined precision, it can't increase the precision of consuming
93 // operations.
94 if (node->getPrecision() < EbpMedium)
95 return;
96
97 // It's possible the node has no effect on the precision of the consuming expression, depending
98 // on the consuming expression, and the precision of the other parameters of the expression.
99 if (!operandAffectsParentOperationPrecision(node))
100 return;
101
102 // Make the constant a precision-qualified named variable to make sure it affects the precision
103 // of the consuming expression.
104 TIntermDeclaration *variableDeclaration = nullptr;
105 TVariable *variable = DeclareTempVariable(mSymbolTable, node, EvqConst, &variableDeclaration);
106 insertStatementInParentBlock(variableDeclaration);
107 queueReplacement(CreateTempSymbolNode(variable), OriginalNode::IS_DROPPED);
108 }
109
110 } // namespace
111
RecordConstantPrecision(TCompiler * compiler,TIntermNode * root,TSymbolTable * symbolTable)112 bool RecordConstantPrecision(TCompiler *compiler, TIntermNode *root, TSymbolTable *symbolTable)
113 {
114 RecordConstantPrecisionTraverser traverser(symbolTable);
115 root->traverse(&traverser);
116 return traverser.updateTree(compiler, root);
117 }
118
119 } // namespace sh
120