Shao | 11e43ec | 2016-08-11 09:54:08 +0800 | [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 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 | |
| 15 | namespace sh |
| 16 | { |
| 17 | |
| 18 | namespace |
| 19 | { |
| 20 | |
| 21 | class Traverser : public TIntermTraverser |
| 22 | { |
| 23 | public: |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 24 | static void Apply(TIntermNode *root, const TSymbolTable &symbolTable, int shaderVersion); |
Shao | 11e43ec | 2016-08-11 09:54:08 +0800 | [diff] [blame] | 25 | |
| 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 | |
| 36 | Traverser::Traverser(const TSymbolTable &symbolTable, int shaderVersion) |
| 37 | : TIntermTraverser(true, false, false), symbolTable(&symbolTable), shaderVersion(shaderVersion) |
| 38 | { |
| 39 | } |
| 40 | |
| 41 | // static |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 42 | void Traverser::Apply(TIntermNode *root, const TSymbolTable &symbolTable, int shaderVersion) |
Shao | 11e43ec | 2016-08-11 09:54:08 +0800 | [diff] [blame] | 43 | { |
| 44 | Traverser traverser(symbolTable, shaderVersion); |
Shao | 11e43ec | 2016-08-11 09:54:08 +0800 | [diff] [blame] | 45 | do |
| 46 | { |
| 47 | traverser.nextIteration(); |
| 48 | root->traverse(&traverser); |
| 49 | if (traverser.mFound) |
| 50 | { |
| 51 | traverser.updateTree(); |
| 52 | } |
| 53 | } while (traverser.mFound); |
| 54 | } |
| 55 | |
| 56 | void Traverser::nextIteration() |
| 57 | { |
| 58 | mFound = false; |
Shao | 11e43ec | 2016-08-11 09:54:08 +0800 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | bool 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 Etuaho | 1ecd14b | 2017-01-26 13:54:15 -0800 | [diff] [blame] | 69 | if (node->getOp() != EOpCallBuiltInFunction) |
Shao | 11e43ec | 2016-08-11 09:54:08 +0800 | [diff] [blame] | 70 | { |
| 71 | return true; |
| 72 | } |
| 73 | |
Olli Etuaho | ec9232b | 2017-03-27 17:01:37 +0300 | [diff] [blame^] | 74 | if (node->getFunctionSymbolInfo()->getName() != "texelFetchOffset") |
Shao | 11e43ec | 2016-08-11 09:54:08 +0800 | [diff] [blame] | 75 | { |
| 76 | return true; |
| 77 | } |
| 78 | |
| 79 | // Potential problem case detected, apply workaround. |
| 80 | const TIntermSequence *sequence = node->getSequence(); |
| 81 | ASSERT(sequence->size() == 4u); |
Shao | 11e43ec | 2016-08-11 09:54:08 +0800 | [diff] [blame] | 82 | |
Olli Etuaho | ec9232b | 2017-03-27 17:01:37 +0300 | [diff] [blame^] | 83 | // 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; |
Shao | 11e43ec | 2016-08-11 09:54:08 +0800 | [diff] [blame] | 87 | |
| 88 | // Create new node that represents the call of function texelFetch. |
Olli Etuaho | 6d40bbd | 2016-09-30 13:49:38 +0100 | [diff] [blame] | 89 | // Its argument list will be: texelFetch(sampler, Position+offset, lod). |
Olli Etuaho | af6fc1b | 2017-01-26 17:45:35 -0800 | [diff] [blame] | 90 | |
| 91 | TIntermSequence *texelFetchArguments = new TIntermSequence(); |
Shao | 11e43ec | 2016-08-11 09:54:08 +0800 | [diff] [blame] | 92 | |
Shao | 11e43ec | 2016-08-11 09:54:08 +0800 | [diff] [blame] | 93 | // sampler |
Olli Etuaho | af6fc1b | 2017-01-26 17:45:35 -0800 | [diff] [blame] | 94 | texelFetchArguments->push_back(sequence->at(0)); |
Shao | 11e43ec | 2016-08-11 09:54:08 +0800 | [diff] [blame] | 95 | |
Shao | 11e43ec | 2016-08-11 09:54:08 +0800 | [diff] [blame] | 96 | // Position |
| 97 | TIntermTyped *texCoordNode = sequence->at(1)->getAsTyped(); |
| 98 | ASSERT(texCoordNode); |
Olli Etuaho | 6d40bbd | 2016-09-30 13:49:38 +0100 | [diff] [blame] | 99 | |
Shao | 11e43ec | 2016-08-11 09:54:08 +0800 | [diff] [blame] | 100 | // offset |
Olli Etuaho | 3272a6d | 2016-08-29 17:54:50 +0300 | [diff] [blame] | 101 | TIntermTyped *offsetNode = nullptr; |
Shao | 11e43ec | 2016-08-11 09:54:08 +0800 | [diff] [blame] | 102 | 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 Etuaho | af6fc1b | 2017-01-26 17:45:35 -0800 | [diff] [blame] | 107 | TIntermSequence *constructOffsetIvecArguments = new TIntermSequence(); |
| 108 | constructOffsetIvecArguments->push_back(sequence->at(3)->getAsTyped()); |
Shao | 11e43ec | 2016-08-11 09:54:08 +0800 | [diff] [blame] | 109 | |
Olli Etuaho | af6fc1b | 2017-01-26 17:45:35 -0800 | [diff] [blame] | 110 | TIntermTyped *zeroNode = TIntermTyped::CreateZero(TType(EbtInt)); |
| 111 | constructOffsetIvecArguments->push_back(zeroNode); |
Shao | 11e43ec | 2016-08-11 09:54:08 +0800 | [diff] [blame] | 112 | |
Olli Etuaho | fe48632 | 2017-03-21 09:30:54 +0000 | [diff] [blame] | 113 | offsetNode = TIntermAggregate::CreateConstructor(texCoordNode->getType(), EOpConstructIVec3, |
| 114 | constructOffsetIvecArguments); |
Olli Etuaho | af6fc1b | 2017-01-26 17:45:35 -0800 | [diff] [blame] | 115 | offsetNode->setLine(texCoordNode->getLine()); |
Shao | 11e43ec | 2016-08-11 09:54:08 +0800 | [diff] [blame] | 116 | } |
| 117 | else |
| 118 | { |
Olli Etuaho | 3272a6d | 2016-08-29 17:54:50 +0300 | [diff] [blame] | 119 | offsetNode = sequence->at(3)->getAsTyped(); |
Shao | 11e43ec | 2016-08-11 09:54:08 +0800 | [diff] [blame] | 120 | } |
Olli Etuaho | 3272a6d | 2016-08-29 17:54:50 +0300 | [diff] [blame] | 121 | |
| 122 | // Position+offset |
| 123 | TIntermBinary *add = new TIntermBinary(EOpAdd, texCoordNode, offsetNode); |
| 124 | add->setLine(texCoordNode->getLine()); |
Olli Etuaho | af6fc1b | 2017-01-26 17:45:35 -0800 | [diff] [blame] | 125 | texelFetchArguments->push_back(add); |
Shao | 11e43ec | 2016-08-11 09:54:08 +0800 | [diff] [blame] | 126 | |
| 127 | // lod |
Olli Etuaho | af6fc1b | 2017-01-26 17:45:35 -0800 | [diff] [blame] | 128 | texelFetchArguments->push_back(sequence->at(2)); |
Olli Etuaho | 6d40bbd | 2016-09-30 13:49:38 +0100 | [diff] [blame] | 129 | |
Olli Etuaho | af6fc1b | 2017-01-26 17:45:35 -0800 | [diff] [blame] | 130 | ASSERT(texelFetchArguments->size() == 3u); |
| 131 | |
Olli Etuaho | ec9232b | 2017-03-27 17:01:37 +0300 | [diff] [blame^] | 132 | // 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 Etuaho | fe48632 | 2017-03-21 09:30:54 +0000 | [diff] [blame] | 137 | TIntermAggregate *texelFetchNode = TIntermAggregate::CreateBuiltInFunctionCall( |
| 138 | *static_cast<const TFunction *>(texelFetchSymbol), texelFetchArguments); |
Olli Etuaho | af6fc1b | 2017-01-26 17:45:35 -0800 | [diff] [blame] | 139 | texelFetchNode->setLine(node->getLine()); |
Shao | 11e43ec | 2016-08-11 09:54:08 +0800 | [diff] [blame] | 140 | |
| 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 Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 149 | void RewriteTexelFetchOffset(TIntermNode *root, const TSymbolTable &symbolTable, int shaderVersion) |
Shao | 11e43ec | 2016-08-11 09:54:08 +0800 | [diff] [blame] | 150 | { |
| 151 | // texelFetchOffset is only valid in GLSL 3.0 and later. |
| 152 | if (shaderVersion < 300) |
| 153 | return; |
| 154 | |
Jiawei-Shao | f979524 | 2016-09-21 15:19:00 +0800 | [diff] [blame] | 155 | Traverser::Apply(root, symbolTable, shaderVersion); |
Shao | 11e43ec | 2016-08-11 09:54:08 +0800 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | } // namespace sh |