blob: 487c909915dd7333c3fb3013a11d3488cd1635d8 [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
Olli Etuahobd674552016-10-06 13:28:42 +010078 if (node->getFunctionSymbolInfo()->getName().compare(0, 16, "texelFetchOffset") != 0)
Shao11e43ec2016-08-11 09:54:08 +080079 {
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.
Olli Etuahobd674552016-10-06 13:28:42 +010088 bool is2DArray = node->getFunctionSymbolInfo()->getName().find("s2a1") != TString::npos;
Shao11e43ec2016-08-11 09:54:08 +080089
90 // Create new argument list from node->getName().
91 // e.g. Get "(is2a1;vi3;i1;" from "texelFetchOffset(is2a1;vi3;i1;vi2;"
Olli Etuahobd674552016-10-06 13:28:42 +010092 TString newArgs = node->getFunctionSymbolInfo()->getName().substr(
93 16, node->getFunctionSymbolInfo()->getName().length() - 20);
Shao11e43ec2016-08-11 09:54:08 +080094 TString newName = "texelFetch" + newArgs;
95 TSymbol *texelFetchSymbol = symbolTable->findBuiltIn(newName, shaderVersion);
96 ASSERT(texelFetchSymbol);
97 int uniqueId = texelFetchSymbol->getUniqueId();
98
99 // Create new node that represents the call of function texelFetch.
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100100 // Its argument list will be: texelFetch(sampler, Position+offset, lod).
Shao11e43ec2016-08-11 09:54:08 +0800101 TIntermAggregate *texelFetchNode = new TIntermAggregate(EOpFunctionCall);
Olli Etuahobd674552016-10-06 13:28:42 +0100102 texelFetchNode->getFunctionSymbolInfo()->setName(newName);
103 texelFetchNode->getFunctionSymbolInfo()->setId(uniqueId);
Shao11e43ec2016-08-11 09:54:08 +0800104 texelFetchNode->setType(node->getType());
105 texelFetchNode->setLine(node->getLine());
106
Shao11e43ec2016-08-11 09:54:08 +0800107 // sampler
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100108 texelFetchNode->getSequence()->push_back(sequence->at(0));
Shao11e43ec2016-08-11 09:54:08 +0800109
Shao11e43ec2016-08-11 09:54:08 +0800110 // Position
111 TIntermTyped *texCoordNode = sequence->at(1)->getAsTyped();
112 ASSERT(texCoordNode);
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100113
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
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100125 constructIVec3Node->getSequence()->push_back(sequence->at(3)->getAsTyped());
Shao11e43ec2016-08-11 09:54:08 +0800126
127 TConstantUnion *zero = new TConstantUnion();
128 zero->setIConst(0);
129 TType *intType = new TType(EbtInt);
130
131 TIntermConstantUnion *zeroNode = new TIntermConstantUnion(zero, *intType);
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100132 constructIVec3Node->getSequence()->push_back(zeroNode);
Shao11e43ec2016-08-11 09:54:08 +0800133
Olli Etuaho3272a6d2016-08-29 17:54:50 +0300134 offsetNode = constructIVec3Node;
Shao11e43ec2016-08-11 09:54:08 +0800135 }
136 else
137 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +0300138 offsetNode = sequence->at(3)->getAsTyped();
Shao11e43ec2016-08-11 09:54:08 +0800139 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +0300140
141 // Position+offset
142 TIntermBinary *add = new TIntermBinary(EOpAdd, texCoordNode, offsetNode);
143 add->setLine(texCoordNode->getLine());
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100144 texelFetchNode->getSequence()->push_back(add);
Shao11e43ec2016-08-11 09:54:08 +0800145
146 // lod
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100147 texelFetchNode->getSequence()->push_back(sequence->at(2));
148
149 ASSERT(texelFetchNode->getSequence()->size() == 3u);
Shao11e43ec2016-08-11 09:54:08 +0800150
151 // Replace the old node by this new node.
152 queueReplacement(node, texelFetchNode, OriginalNode::IS_DROPPED);
153 mFound = true;
154 return false;
155}
156
157} // anonymous namespace
158
159void RewriteTexelFetchOffset(TIntermNode *root,
Shao11e43ec2016-08-11 09:54:08 +0800160 const TSymbolTable &symbolTable,
161 int shaderVersion)
162{
163 // texelFetchOffset is only valid in GLSL 3.0 and later.
164 if (shaderVersion < 300)
165 return;
166
Jiawei-Shaof9795242016-09-21 15:19:00 +0800167 Traverser::Apply(root, symbolTable, shaderVersion);
Shao11e43ec2016-08-11 09:54:08 +0800168}
169
170} // namespace sh