Commit cf180fcc by Olli Etuaho Committed by Commit Bot

Keep TIntermSymbol data consistent in DeferGlobalInitializers

BUG=angleproject:2267 TEST=angle_unittests Change-Id: I25bd8baded9c13e75555578e4b61b99a56e0c702 Reviewed-on: https://chromium-review.googlesource.com/850974Reviewed-by: 's avatarCorentin Wallez <cwallez@chromium.org> Reviewed-by: 's avatarGeoff Lang <geofflang@chromium.org> Commit-Queue: Olli Etuaho <oetuaho@nvidia.com>
parent dda048cd
...@@ -121,6 +121,8 @@ ...@@ -121,6 +121,8 @@
'compiler/translator/RemovePow.h', 'compiler/translator/RemovePow.h',
'compiler/translator/RemoveUnreferencedVariables.cpp', 'compiler/translator/RemoveUnreferencedVariables.cpp',
'compiler/translator/RemoveUnreferencedVariables.h', 'compiler/translator/RemoveUnreferencedVariables.h',
'compiler/translator/ReplaceVariable.cpp',
'compiler/translator/ReplaceVariable.h',
'compiler/translator/RewriteDoWhile.cpp', 'compiler/translator/RewriteDoWhile.cpp',
'compiler/translator/RewriteDoWhile.h', 'compiler/translator/RewriteDoWhile.h',
'compiler/translator/RewriteTexelFetchOffset.cpp', 'compiler/translator/RewriteTexelFetchOffset.cpp',
......
...@@ -13,6 +13,7 @@ ...@@ -13,6 +13,7 @@
#include "compiler/translator/InitializeVariables.h" #include "compiler/translator/InitializeVariables.h"
#include "compiler/translator/IntermNode_util.h" #include "compiler/translator/IntermNode_util.h"
#include "compiler/translator/IntermTraverse.h" #include "compiler/translator/IntermTraverse.h"
#include "compiler/translator/ReplaceVariable.h"
#include "compiler/translator/SymbolTable.h" #include "compiler/translator/SymbolTable.h"
#include "compiler/translator/util.h" #include "compiler/translator/util.h"
...@@ -22,29 +23,6 @@ namespace sh ...@@ -22,29 +23,6 @@ namespace sh
namespace namespace
{ {
class ReplaceVariableTraverser : public TIntermTraverser
{
public:
ReplaceVariableTraverser(const TVariable *toBeReplaced, const TVariable *replacement)
: TIntermTraverser(true, false, false),
mToBeReplaced(toBeReplaced),
mReplacement(replacement)
{
}
void visitSymbol(TIntermSymbol *node) override
{
if (&node->variable() == mToBeReplaced)
{
queueReplacement(new TIntermSymbol(mReplacement), OriginalNode::IS_DROPPED);
}
}
private:
const TVariable *const mToBeReplaced;
const TVariable *const mReplacement;
};
TIntermSymbol *CreateGLInstanceIDSymbol(const TSymbolTable &symbolTable) TIntermSymbol *CreateGLInstanceIDSymbol(const TSymbolTable &symbolTable)
{ {
return ReferenceBuiltInVariable("gl_InstanceID", symbolTable, 300); return ReferenceBuiltInVariable("gl_InstanceID", symbolTable, 300);
...@@ -94,14 +72,6 @@ void InitializeViewIDAndInstanceID(const TVariable *viewID, ...@@ -94,14 +72,6 @@ void InitializeViewIDAndInstanceID(const TVariable *viewID,
initializers->push_back(viewIDInitializer); initializers->push_back(viewIDInitializer);
} }
// Replaces every occurrence of a variable with another variable.
void ReplaceSymbol(TIntermBlock *root, const TVariable *toBeReplaced, const TVariable *replacement)
{
ReplaceVariableTraverser traverser(toBeReplaced, replacement);
root->traverse(&traverser);
traverser.updateTree();
}
void DeclareGlobalVariable(TIntermBlock *root, const TVariable *variable) void DeclareGlobalVariable(TIntermBlock *root, const TVariable *variable)
{ {
TIntermDeclaration *declaration = new TIntermDeclaration(); TIntermDeclaration *declaration = new TIntermDeclaration();
...@@ -176,9 +146,9 @@ void DeclareAndInitBuiltinsForInstancedMultiview(TIntermBlock *root, ...@@ -176,9 +146,9 @@ void DeclareAndInitBuiltinsForInstancedMultiview(TIntermBlock *root,
SymbolType::AngleInternal); SymbolType::AngleInternal);
DeclareGlobalVariable(root, viewID); DeclareGlobalVariable(root, viewID);
ReplaceSymbol(root, ReplaceVariable(root,
static_cast<TVariable *>(symbolTable->findBuiltIn("gl_ViewID_OVR", 300, true)), static_cast<TVariable *>(symbolTable->findBuiltIn("gl_ViewID_OVR", 300, true)),
viewID); viewID);
if (shaderType == GL_VERTEX_SHADER) if (shaderType == GL_VERTEX_SHADER)
{ {
// Replacing gl_InstanceID with InstanceID should happen before adding the initializers of // Replacing gl_InstanceID with InstanceID should happen before adding the initializers of
...@@ -188,7 +158,7 @@ void DeclareAndInitBuiltinsForInstancedMultiview(TIntermBlock *root, ...@@ -188,7 +158,7 @@ void DeclareAndInitBuiltinsForInstancedMultiview(TIntermBlock *root,
new TVariable(symbolTable, instanceIDVariableName, TType(EbtInt, EbpHigh, EvqGlobal), new TVariable(symbolTable, instanceIDVariableName, TType(EbtInt, EbpHigh, EvqGlobal),
SymbolType::AngleInternal); SymbolType::AngleInternal);
DeclareGlobalVariable(root, instanceID); DeclareGlobalVariable(root, instanceID);
ReplaceSymbol( ReplaceVariable(
root, static_cast<TVariable *>(symbolTable->findBuiltIn("gl_InstanceID", 300, true)), root, static_cast<TVariable *>(symbolTable->findBuiltIn("gl_InstanceID", 300, true)),
instanceID); instanceID);
......
...@@ -15,10 +15,13 @@ ...@@ -15,10 +15,13 @@
#include "compiler/translator/DeferGlobalInitializers.h" #include "compiler/translator/DeferGlobalInitializers.h"
#include <vector>
#include "compiler/translator/FindMain.h" #include "compiler/translator/FindMain.h"
#include "compiler/translator/InitializeVariables.h" #include "compiler/translator/InitializeVariables.h"
#include "compiler/translator/IntermNode.h" #include "compiler/translator/IntermNode.h"
#include "compiler/translator/IntermNode_util.h" #include "compiler/translator/IntermNode_util.h"
#include "compiler/translator/ReplaceVariable.h"
#include "compiler/translator/SymbolTable.h" #include "compiler/translator/SymbolTable.h"
namespace sh namespace sh
...@@ -32,6 +35,7 @@ void GetDeferredInitializers(TIntermDeclaration *declaration, ...@@ -32,6 +35,7 @@ void GetDeferredInitializers(TIntermDeclaration *declaration,
bool canUseLoopsToInitialize, bool canUseLoopsToInitialize,
bool highPrecisionSupported, bool highPrecisionSupported,
TIntermSequence *deferredInitializersOut, TIntermSequence *deferredInitializersOut,
std::vector<const TVariable *> *variablesToReplaceOut,
TSymbolTable *symbolTable) TSymbolTable *symbolTable)
{ {
// SeparateDeclarations should have already been run. // SeparateDeclarations should have already been run.
...@@ -60,7 +64,7 @@ void GetDeferredInitializers(TIntermDeclaration *declaration, ...@@ -60,7 +64,7 @@ void GetDeferredInitializers(TIntermDeclaration *declaration,
symbolNode->getQualifier() == EvqGlobal); symbolNode->getQualifier() == EvqGlobal);
if (symbolNode->getQualifier() == EvqConst) if (symbolNode->getQualifier() == EvqConst)
{ {
symbolNode->getTypePointer()->setQualifier(EvqGlobal); variablesToReplaceOut->push_back(&symbolNode->variable());
} }
TIntermBinary *deferredInit = TIntermBinary *deferredInit =
...@@ -125,6 +129,7 @@ void DeferGlobalInitializers(TIntermBlock *root, ...@@ -125,6 +129,7 @@ void DeferGlobalInitializers(TIntermBlock *root,
TSymbolTable *symbolTable) TSymbolTable *symbolTable)
{ {
TIntermSequence *deferredInitializers = new TIntermSequence(); TIntermSequence *deferredInitializers = new TIntermSequence();
std::vector<const TVariable *> variablesToReplace;
// Loop over all global statements and process the declarations. This is simpler than using a // Loop over all global statements and process the declarations. This is simpler than using a
// traverser. // traverser.
...@@ -135,7 +140,7 @@ void DeferGlobalInitializers(TIntermBlock *root, ...@@ -135,7 +140,7 @@ void DeferGlobalInitializers(TIntermBlock *root,
{ {
GetDeferredInitializers(declaration, initializeUninitializedGlobals, GetDeferredInitializers(declaration, initializeUninitializedGlobals,
canUseLoopsToInitialize, highPrecisionSupported, canUseLoopsToInitialize, highPrecisionSupported,
deferredInitializers, symbolTable); deferredInitializers, &variablesToReplace, symbolTable);
} }
} }
...@@ -144,6 +149,16 @@ void DeferGlobalInitializers(TIntermBlock *root, ...@@ -144,6 +149,16 @@ void DeferGlobalInitializers(TIntermBlock *root,
{ {
InsertInitCallToMain(root, deferredInitializers, symbolTable); InsertInitCallToMain(root, deferredInitializers, symbolTable);
} }
// Replace constant variables with non-constant global variables.
for (const TVariable *var : variablesToReplace)
{
TType replacementType(var->getType());
replacementType.setQualifier(EvqGlobal);
TVariable *replacement =
new TVariable(symbolTable, &var->name(), replacementType, var->symbolType());
ReplaceVariable(root, var, replacement);
}
} }
} // namespace sh } // namespace sh
//
// Copyright (c) 2018 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.
//
// ReplaceVariable.cpp: Replace all references to a specific variable in the AST with references to
// another variable.
#include "compiler/translator/ReplaceVariable.h"
#include "compiler/translator/IntermNode.h"
#include "compiler/translator/IntermTraverse.h"
namespace sh
{
namespace
{
class ReplaceVariableTraverser : public TIntermTraverser
{
public:
ReplaceVariableTraverser(const TVariable *toBeReplaced, const TVariable *replacement)
: TIntermTraverser(true, false, false),
mToBeReplaced(toBeReplaced),
mReplacement(replacement)
{
}
void visitSymbol(TIntermSymbol *node) override
{
if (&node->variable() == mToBeReplaced)
{
queueReplacement(new TIntermSymbol(mReplacement), OriginalNode::IS_DROPPED);
}
}
private:
const TVariable *const mToBeReplaced;
const TVariable *const mReplacement;
};
} // anonymous namespace
// Replaces every occurrence of a variable with another variable.
void ReplaceVariable(TIntermBlock *root,
const TVariable *toBeReplaced,
const TVariable *replacement)
{
ReplaceVariableTraverser traverser(toBeReplaced, replacement);
root->traverse(&traverser);
traverser.updateTree();
}
} // namespace sh
//
// Copyright (c) 2018 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.
//
// ReplaceVariable.h: Replace all references to a specific variable in the AST with references to
// another variable.
#ifndef COMPILER_TRANSLATOR_REPLACEVARIABLE_H_
#define COMPILER_TRANSLATOR_REPLACEVARIABLE_H_
namespace sh
{
class TIntermBlock;
class TVariable;
void ReplaceVariable(TIntermBlock *root,
const TVariable *toBeReplaced,
const TVariable *replacement);
} // namespace sh
#endif // COMPILER_TRANSLATOR_REPLACEVARIABLE_H_
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment