blob: bcd0bb5b21b900c5bd44846ec3458691f3525ab8 [file] [log] [blame]
Shao11e43ec2016-08-11 09:54:08 +08001//
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 texelFetchOffset translation issue workaround.
7// See header for more info.
8
9#include "compiler/translator/RewriteTexelFetchOffset.h"
10
11#include "common/angleutils.h"
12#include "compiler/translator/IntermNode.h"
13#include "compiler/translator/SymbolTable.h"
14
15namespace sh
16{
17
18namespace
19{
20
21class Traverser : public TIntermTraverser
22{
23 public:
24 static void Apply(TIntermNode *root,
Shao11e43ec2016-08-11 09:54:08 +080025 const TSymbolTable &symbolTable,
26 int shaderVersion);
27
28 private:
29 Traverser(const TSymbolTable &symbolTable, int shaderVersion);
30 bool visitAggregate(Visit visit, TIntermAggregate *node) override;
31 void nextIteration();
32
33 const TSymbolTable *symbolTable;
34 const int shaderVersion;
35 bool mFound = false;
36};
37
38Traverser::Traverser(const TSymbolTable &symbolTable, int shaderVersion)
39 : TIntermTraverser(true, false, false), symbolTable(&symbolTable), shaderVersion(shaderVersion)
40{
41}
42
43// static
44void Traverser::Apply(TIntermNode *root,
Shao11e43ec2016-08-11 09:54:08 +080045 const TSymbolTable &symbolTable,
46 int shaderVersion)
47{
48 Traverser traverser(symbolTable, shaderVersion);
Shao11e43ec2016-08-11 09:54:08 +080049 do
50 {
51 traverser.nextIteration();
52 root->traverse(&traverser);
53 if (traverser.mFound)
54 {
55 traverser.updateTree();
56 }
57 } while (traverser.mFound);
58}
59
60void Traverser::nextIteration()
61{
62 mFound = false;
Shao11e43ec2016-08-11 09:54:08 +080063}
64
65bool Traverser::visitAggregate(Visit visit, TIntermAggregate *node)
66{
67 if (mFound)
68 {
69 return false;
70 }
71
72 // Decide if the node represents the call of texelFetchOffset.
73 if (node->getOp() != EOpFunctionCall || node->isUserDefined())
74 {
75 return true;
76 }
77
78 if (node->getName().compare(0, 16, "texelFetchOffset") != 0)
79 {
80 return true;
81 }
82
83 // Potential problem case detected, apply workaround.
84 const TIntermSequence *sequence = node->getSequence();
85 ASSERT(sequence->size() == 4u);
Shao11e43ec2016-08-11 09:54:08 +080086
87 // Decide if there is a 2DArray sampler.
88 bool is2DArray = node->getName().find("s2a1") != TString::npos;
89
90 // Create new argument list from node->getName().
91 // e.g. Get "(is2a1;vi3;i1;" from "texelFetchOffset(is2a1;vi3;i1;vi2;"
92 TString newArgs = node->getName().substr(16, node->getName().length() - 20);
93 TString newName = "texelFetch" + newArgs;
94 TSymbol *texelFetchSymbol = symbolTable->findBuiltIn(newName, shaderVersion);
95 ASSERT(texelFetchSymbol);
96 int uniqueId = texelFetchSymbol->getUniqueId();
97
98 // Create new node that represents the call of function texelFetch.
99 TIntermAggregate *texelFetchNode = new TIntermAggregate(EOpFunctionCall);
100 texelFetchNode->setName(newName);
101 texelFetchNode->setFunctionId(uniqueId);
102 texelFetchNode->setType(node->getType());
103 texelFetchNode->setLine(node->getLine());
104
105 // Create argument List of texelFetch(sampler, Position+offset, lod).
106 TIntermSequence newsequence;
107
108 // sampler
109 newsequence.push_back(sequence->at(0));
110
Shao11e43ec2016-08-11 09:54:08 +0800111 // Position
112 TIntermTyped *texCoordNode = sequence->at(1)->getAsTyped();
113 ASSERT(texCoordNode);
Shao11e43ec2016-08-11 09:54:08 +0800114 // offset
Olli Etuaho3272a6d2016-08-29 17:54:50 +0300115 TIntermTyped *offsetNode = nullptr;
Shao11e43ec2016-08-11 09:54:08 +0800116 ASSERT(sequence->at(3)->getAsTyped());
117 if (is2DArray)
118 {
119 // For 2DArray samplers, Position is ivec3 and offset is ivec2;
120 // So offset must be converted into an ivec3 before being added to Position.
121 TIntermAggregate *constructIVec3Node = new TIntermAggregate(EOpConstructIVec3);
122 constructIVec3Node->setLine(texCoordNode->getLine());
123 constructIVec3Node->setType(texCoordNode->getType());
124
125 TIntermSequence ivec3Sequence;
126 ivec3Sequence.push_back(sequence->at(3)->getAsTyped());
127
128 TConstantUnion *zero = new TConstantUnion();
129 zero->setIConst(0);
130 TType *intType = new TType(EbtInt);
131
132 TIntermConstantUnion *zeroNode = new TIntermConstantUnion(zero, *intType);
133 ivec3Sequence.push_back(zeroNode);
134 constructIVec3Node->insertChildNodes(0, ivec3Sequence);
135
Olli Etuaho3272a6d2016-08-29 17:54:50 +0300136 offsetNode = constructIVec3Node;
Shao11e43ec2016-08-11 09:54:08 +0800137 }
138 else
139 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +0300140 offsetNode = sequence->at(3)->getAsTyped();
Shao11e43ec2016-08-11 09:54:08 +0800141 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +0300142
143 // Position+offset
144 TIntermBinary *add = new TIntermBinary(EOpAdd, texCoordNode, offsetNode);
145 add->setLine(texCoordNode->getLine());
Shao11e43ec2016-08-11 09:54:08 +0800146 newsequence.push_back(add);
147
148 // lod
149 newsequence.push_back(sequence->at(2));
150 texelFetchNode->insertChildNodes(0, newsequence);
151
152 // Replace the old node by this new node.
153 queueReplacement(node, texelFetchNode, OriginalNode::IS_DROPPED);
154 mFound = true;
155 return false;
156}
157
158} // anonymous namespace
159
160void RewriteTexelFetchOffset(TIntermNode *root,
Shao11e43ec2016-08-11 09:54:08 +0800161 const TSymbolTable &symbolTable,
162 int shaderVersion)
163{
164 // texelFetchOffset is only valid in GLSL 3.0 and later.
165 if (shaderVersion < 300)
166 return;
167
Jiawei-Shaof9795242016-09-21 15:19:00 +0800168 Traverser::Apply(root, symbolTable, shaderVersion);
Shao11e43ec2016-08-11 09:54:08 +0800169}
170
171} // namespace sh