Hash :
ea39a223
Author :
Date :
2017-07-06T12:47:59
Simplify queueReplacement AST transform helper queueReplacement is always called to replace the node that's currently being visited in the traverser. The currently visited node can be fetched automatically from the traversal path so it can be removed from parameters of queueReplacement. BUG=angleproject:2100 TEST=angle_unittests Change-Id: I62ab6d1cd9c0d2b4c260af9f7c85bc156fb3f349 Reviewed-on: https://chromium-review.googlesource.com/562336 Reviewed-by: Corentin Wallez <cwallez@chromium.org> Reviewed-by: Jamie Madill <jmadill@chromium.org> Commit-Queue: Olli Etuaho <oetuaho@nvidia.com>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
//
// Copyright (c) 2017 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// Applies the necessary AST transformations to support multiview rendering through instancing.
// Check the header file For more information.
//
#include "compiler/translator/DeclareAndInitBuiltinsForInstancedMultiview.h"
#include "compiler/translator/FindMain.h"
#include "compiler/translator/InitializeVariables.h"
#include "compiler/translator/IntermTraverse.h"
#include "compiler/translator/SymbolTable.h"
namespace sh
{
namespace
{
class ReplaceVariableTraverser : public TIntermTraverser
{
public:
ReplaceVariableTraverser(const TString &symbolName, TIntermSymbol *newSymbol)
: TIntermTraverser(true, false, false), mSymbolName(symbolName), mNewSymbol(newSymbol)
{
}
void visitSymbol(TIntermSymbol *node) override
{
TName &name = node->getName();
if (name.getString() == mSymbolName)
{
queueReplacement(mNewSymbol->deepCopy(), OriginalNode::IS_DROPPED);
}
}
private:
TString mSymbolName;
TIntermSymbol *mNewSymbol;
};
TIntermSymbol *CreateGLInstanceIDSymbol()
{
return new TIntermSymbol(0, "gl_InstanceID", TType(EbtInt, EbpHigh, EvqInstanceID));
}
void InitializeViewIDAndInstanceID(TIntermBlock *root,
TIntermTyped *viewIDSymbol,
TIntermTyped *instanceIDSymbol,
unsigned numberOfViews)
{
// Create a signed numberOfViews node.
TConstantUnion *numberOfViewsConstant = new TConstantUnion();
numberOfViewsConstant->setIConst(static_cast<int>(numberOfViews));
TIntermConstantUnion *numberOfViewsIntSymbol =
new TIntermConstantUnion(numberOfViewsConstant, TType(EbtInt, EbpHigh, EvqConst));
// Create a gl_InstanceID / numberOfViews node.
TIntermBinary *normalizedInstanceID =
new TIntermBinary(EOpDiv, CreateGLInstanceIDSymbol(), numberOfViewsIntSymbol);
// Create a InstanceID = gl_InstanceID / numberOfViews node.
TIntermBinary *instanceIDInitializer =
new TIntermBinary(EOpAssign, instanceIDSymbol->deepCopy(), normalizedInstanceID);
// Create a uint(gl_InstanceID) node.
TIntermSequence *glInstanceIDSymbolCastArguments = new TIntermSequence();
glInstanceIDSymbolCastArguments->push_back(CreateGLInstanceIDSymbol());
TIntermAggregate *glInstanceIDAsUint = TIntermAggregate::CreateConstructor(
TType(EbtUInt, EbpHigh, EvqTemporary), glInstanceIDSymbolCastArguments);
// Create an unsigned numberOfViews node.
TConstantUnion *numberOfViewsUnsignedConstant = new TConstantUnion();
numberOfViewsUnsignedConstant->setUConst(numberOfViews);
TIntermConstantUnion *numberOfViewsUintSymbol =
new TIntermConstantUnion(numberOfViewsUnsignedConstant, TType(EbtUInt, EbpHigh, EvqConst));
// Create a uint(gl_InstanceID) % numberOfViews node.
TIntermBinary *normalizedViewID =
new TIntermBinary(EOpIMod, glInstanceIDAsUint, numberOfViewsUintSymbol);
// Create a ViewID_OVR = uint(gl_InstanceID) % numberOfViews node.
TIntermBinary *viewIDInitializer =
new TIntermBinary(EOpAssign, viewIDSymbol->deepCopy(), normalizedViewID);
// Add initializers at the beginning of main().
TIntermBlock *mainBody = FindMainBody(root);
mainBody->getSequence()->insert(mainBody->getSequence()->begin(), instanceIDInitializer);
mainBody->getSequence()->insert(mainBody->getSequence()->begin(), viewIDInitializer);
}
// Replaces every occurrence of a symbol with the name specified in symbolName with newSymbolNode.
void ReplaceSymbol(TIntermBlock *root, const TString &symbolName, TIntermSymbol *newSymbolNode)
{
ReplaceVariableTraverser traverser(symbolName, newSymbolNode);
root->traverse(&traverser);
traverser.updateTree();
}
void DeclareGlobalVariable(TIntermBlock *root, TIntermTyped *typedNode)
{
TIntermSequence *globalSequence = root->getSequence();
TIntermDeclaration *declaration = new TIntermDeclaration();
declaration->appendDeclarator(typedNode->deepCopy());
globalSequence->insert(globalSequence->begin(), declaration);
}
} // namespace
void DeclareAndInitBuiltinsForInstancedMultiview(TIntermBlock *root,
unsigned numberOfViews,
GLenum shaderType)
{
ASSERT(shaderType == GL_VERTEX_SHADER || shaderType == GL_FRAGMENT_SHADER);
TQualifier viewIDQualifier = (shaderType == GL_VERTEX_SHADER) ? EvqFlatOut : EvqFlatIn;
TIntermSymbol *viewIDSymbol = new TIntermSymbol(TSymbolTable::nextUniqueId(), "ViewID_OVR",
TType(EbtUInt, EbpHigh, viewIDQualifier));
viewIDSymbol->setInternal(true);
DeclareGlobalVariable(root, viewIDSymbol);
ReplaceSymbol(root, "gl_ViewID_OVR", viewIDSymbol);
if (shaderType == GL_VERTEX_SHADER)
{
// Replacing gl_InstanceID with InstanceID should happen before adding the initializers of
// InstanceID and ViewID.
TIntermSymbol *instanceIDSymbol = new TIntermSymbol(
TSymbolTable::nextUniqueId(), "InstanceID", TType(EbtInt, EbpHigh, EvqGlobal));
instanceIDSymbol->setInternal(true);
DeclareGlobalVariable(root, instanceIDSymbol);
ReplaceSymbol(root, "gl_InstanceID", instanceIDSymbol);
InitializeViewIDAndInstanceID(root, viewIDSymbol, instanceIDSymbol, numberOfViews);
}
}
} // namespace sh