|
| 1 | +// |
| 2 | +// Copyright 2020 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 | +// FlagSamplersForTexelFetch.cpp: finds all instances of texelFetch used with a static reference to |
| 7 | +// a sampler uniform, and flag that uniform as having been used with texelFetch |
| 8 | +// |
| 9 | + |
| 10 | +#include "compiler/translator/tree_ops/FlagSamplersWithTexelFetch.h" |
| 11 | + |
| 12 | +#include "angle_gl.h" |
| 13 | +#include "common/utilities.h" |
| 14 | + |
| 15 | +#include "compiler/translator/SymbolTable.h" |
| 16 | +#include "compiler/translator/tree_util/IntermNode_util.h" |
| 17 | +#include "compiler/translator/tree_util/IntermTraverse.h" |
| 18 | +#include "compiler/translator/tree_util/ReplaceVariable.h" |
| 19 | + |
| 20 | +namespace sh |
| 21 | +{ |
| 22 | +namespace |
| 23 | +{ |
| 24 | + |
| 25 | +// gvec4 texelFetch(gsamplerXD sampler, ivecX P, int lod) |
| 26 | +constexpr uint32_t kTexelFetchSequenceLength = 3u; |
| 27 | + |
| 28 | +// gvec4 texelFetchOffset(gsamplerXD sampler, ivecX P, int lod, ivecX offset) |
| 29 | +constexpr uint32_t kTexelFetchOffsetSequenceLength = 4u; |
| 30 | + |
| 31 | +class FlagSamplersWithTexelFetchTraverser : public TIntermTraverser |
| 32 | +{ |
| 33 | + public: |
| 34 | + FlagSamplersWithTexelFetchTraverser(TSymbolTable *symbolTable, |
| 35 | + std::vector<ShaderVariable> *uniforms) |
| 36 | + : TIntermTraverser(true, true, true, symbolTable), mUniforms(uniforms) |
| 37 | + {} |
| 38 | + |
| 39 | + bool visitAggregate(Visit visit, TIntermAggregate *node) override |
| 40 | + { |
| 41 | + // Decide if the node is a call to texelFetch[Offset] |
| 42 | + if (node->getOp() != EOpCallBuiltInFunction) |
| 43 | + { |
| 44 | + return true; |
| 45 | + } |
| 46 | + |
| 47 | + ASSERT(node->getFunction()->symbolType() == SymbolType::BuiltIn); |
| 48 | + if (node->getFunction()->name() != "texelFetch" && |
| 49 | + node->getFunction()->name() != "texelFetchOffset") |
| 50 | + { |
| 51 | + return true; |
| 52 | + } |
| 53 | + |
| 54 | + const TIntermSequence *sequence = node->getSequence(); |
| 55 | + |
| 56 | + // This must be a valid texelFetch invokation with the required number of arguments |
| 57 | + ASSERT(sequence->size() == (node->getFunction()->name() == "texelFetch" |
| 58 | + ? kTexelFetchSequenceLength |
| 59 | + : kTexelFetchOffsetSequenceLength)); |
| 60 | + |
| 61 | + TIntermSymbol *samplerSymbol = sequence->at(0)->getAsSymbolNode(); |
| 62 | + ASSERT(samplerSymbol != nullptr); |
| 63 | + |
| 64 | + const TVariable &samplerVariable = samplerSymbol->variable(); |
| 65 | + |
| 66 | + for (ShaderVariable &uniform : *mUniforms) |
| 67 | + { |
| 68 | + if (samplerVariable.name() == uniform.name) |
| 69 | + { |
| 70 | + ASSERT(gl::IsSamplerType(uniform.type)); |
| 71 | + uniform.texelFetchInvoked = true; |
| 72 | + break; |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + return true; |
| 77 | + } |
| 78 | + |
| 79 | + private: |
| 80 | + std::vector<ShaderVariable> *mUniforms; |
| 81 | +}; |
| 82 | + |
| 83 | +} // anonymous namespace |
| 84 | + |
| 85 | +bool FlagSamplersForTexelFetch(TCompiler *compiler, |
| 86 | + TIntermBlock *root, |
| 87 | + TSymbolTable *symbolTable, |
| 88 | + std::vector<ShaderVariable> *uniforms) |
| 89 | +{ |
| 90 | + ASSERT(uniforms != nullptr); |
| 91 | + if (uniforms->size() > 0) |
| 92 | + { |
| 93 | + FlagSamplersWithTexelFetchTraverser traverser(symbolTable, uniforms); |
| 94 | + root->traverse(&traverser); |
| 95 | + } |
| 96 | + |
| 97 | + return true; |
| 98 | +} |
| 99 | + |
| 100 | +} // namespace sh |
0 commit comments