blob: 554b044ecf75fed8d5e14cb85a142f2142b05776 [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:
Jamie Madilld7b1ab52016-12-12 14:42:19 -050024 static void Apply(TIntermNode *root, const TSymbolTable &symbolTable, int shaderVersion);
Shao11e43ec2016-08-11 09:54:08 +080025
26 private:
27 Traverser(const TSymbolTable &symbolTable, int shaderVersion);
28 bool visitAggregate(Visit visit, TIntermAggregate *node) override;
29 void nextIteration();
30
31 const TSymbolTable *symbolTable;
32 const int shaderVersion;
33 bool mFound = false;
34};
35
36Traverser::Traverser(const TSymbolTable &symbolTable, int shaderVersion)
37 : TIntermTraverser(true, false, false), symbolTable(&symbolTable), shaderVersion(shaderVersion)
38{
39}
40
41// static
Jamie Madilld7b1ab52016-12-12 14:42:19 -050042void Traverser::Apply(TIntermNode *root, const TSymbolTable &symbolTable, int shaderVersion)
Shao11e43ec2016-08-11 09:54:08 +080043{
44 Traverser traverser(symbolTable, shaderVersion);
Shao11e43ec2016-08-11 09:54:08 +080045 do
46 {
47 traverser.nextIteration();
48 root->traverse(&traverser);
49 if (traverser.mFound)
50 {
51 traverser.updateTree();
52 }
53 } while (traverser.mFound);
54}
55
56void Traverser::nextIteration()
57{
58 mFound = false;
Shao11e43ec2016-08-11 09:54:08 +080059}
60
61bool Traverser::visitAggregate(Visit visit, TIntermAggregate *node)
62{
63 if (mFound)
64 {
65 return false;
66 }
67
68 // Decide if the node represents the call of texelFetchOffset.
Olli Etuaho1ecd14b2017-01-26 13:54:15 -080069 if (node->getOp() != EOpCallBuiltInFunction)
Shao11e43ec2016-08-11 09:54:08 +080070 {
71 return true;
72 }
73
Olli Etuahoec9232b2017-03-27 17:01:37 +030074 if (node->getFunctionSymbolInfo()->getName() != "texelFetchOffset")
Shao11e43ec2016-08-11 09:54:08 +080075 {
76 return true;
77 }
78
79 // Potential problem case detected, apply workaround.
80 const TIntermSequence *sequence = node->getSequence();
81 ASSERT(sequence->size() == 4u);
Shao11e43ec2016-08-11 09:54:08 +080082
Olli Etuahoec9232b2017-03-27 17:01:37 +030083 // Decide if the sampler is a 2DArray sampler. In that case position is ivec3 and offset is
84 // ivec2.
85 bool is2DArray = sequence->at(1)->getAsTyped()->getNominalSize() == 3 &&
86 sequence->at(3)->getAsTyped()->getNominalSize() == 2;
Shao11e43ec2016-08-11 09:54:08 +080087
88 // Create new node that represents the call of function texelFetch.
Olli Etuaho6d40bbd2016-09-30 13:49:38 +010089 // Its argument list will be: texelFetch(sampler, Position+offset, lod).
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -080090
91 TIntermSequence *texelFetchArguments = new TIntermSequence();
Shao11e43ec2016-08-11 09:54:08 +080092
Shao11e43ec2016-08-11 09:54:08 +080093 // sampler
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -080094 texelFetchArguments->push_back(sequence->at(0));
Shao11e43ec2016-08-11 09:54:08 +080095
Shao11e43ec2016-08-11 09:54:08 +080096 // Position
97 TIntermTyped *texCoordNode = sequence->at(1)->getAsTyped();
98 ASSERT(texCoordNode);
Olli Etuaho6d40bbd2016-09-30 13:49:38 +010099
Shao11e43ec2016-08-11 09:54:08 +0800100 // offset
Olli Etuaho3272a6d2016-08-29 17:54:50 +0300101 TIntermTyped *offsetNode = nullptr;
Shao11e43ec2016-08-11 09:54:08 +0800102 ASSERT(sequence->at(3)->getAsTyped());
103 if (is2DArray)
104 {
105 // For 2DArray samplers, Position is ivec3 and offset is ivec2;
106 // So offset must be converted into an ivec3 before being added to Position.
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800107 TIntermSequence *constructOffsetIvecArguments = new TIntermSequence();
108 constructOffsetIvecArguments->push_back(sequence->at(3)->getAsTyped());
Shao11e43ec2016-08-11 09:54:08 +0800109
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800110 TIntermTyped *zeroNode = TIntermTyped::CreateZero(TType(EbtInt));
111 constructOffsetIvecArguments->push_back(zeroNode);
Shao11e43ec2016-08-11 09:54:08 +0800112
Olli Etuahofe486322017-03-21 09:30:54 +0000113 offsetNode = TIntermAggregate::CreateConstructor(texCoordNode->getType(), EOpConstructIVec3,
114 constructOffsetIvecArguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800115 offsetNode->setLine(texCoordNode->getLine());
Shao11e43ec2016-08-11 09:54:08 +0800116 }
117 else
118 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +0300119 offsetNode = sequence->at(3)->getAsTyped();
Shao11e43ec2016-08-11 09:54:08 +0800120 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +0300121
122 // Position+offset
123 TIntermBinary *add = new TIntermBinary(EOpAdd, texCoordNode, offsetNode);
124 add->setLine(texCoordNode->getLine());
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800125 texelFetchArguments->push_back(add);
Shao11e43ec2016-08-11 09:54:08 +0800126
127 // lod
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800128 texelFetchArguments->push_back(sequence->at(2));
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100129
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800130 ASSERT(texelFetchArguments->size() == 3u);
131
Olli Etuahoec9232b2017-03-27 17:01:37 +0300132 // Get the symbol of the texel fetch function to use.
133 TString mangledName = TFunction::GetMangledNameFromCall("texelFetch", *texelFetchArguments);
134 TSymbol *texelFetchSymbol = symbolTable->findBuiltIn(mangledName, shaderVersion);
135 ASSERT(texelFetchSymbol && texelFetchSymbol->isFunction());
136
Olli Etuahofe486322017-03-21 09:30:54 +0000137 TIntermAggregate *texelFetchNode = TIntermAggregate::CreateBuiltInFunctionCall(
138 *static_cast<const TFunction *>(texelFetchSymbol), texelFetchArguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800139 texelFetchNode->setLine(node->getLine());
Shao11e43ec2016-08-11 09:54:08 +0800140
141 // Replace the old node by this new node.
142 queueReplacement(node, texelFetchNode, OriginalNode::IS_DROPPED);
143 mFound = true;
144 return false;
145}
146
147} // anonymous namespace
148
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500149void RewriteTexelFetchOffset(TIntermNode *root, const TSymbolTable &symbolTable, int shaderVersion)
Shao11e43ec2016-08-11 09:54:08 +0800150{
151 // texelFetchOffset is only valid in GLSL 3.0 and later.
152 if (shaderVersion < 300)
153 return;
154
Jiawei-Shaof9795242016-09-21 15:19:00 +0800155 Traverser::Apply(root, symbolTable, shaderVersion);
Shao11e43ec2016-08-11 09:54:08 +0800156}
157
158} // namespace sh