Commit df7d7c9e by Jiawei-Shao Committed by Commit Bot

Split varyings into input and output varyings in compiler

This patch intends to split all vector<Varying> into two vectors to store input and output varyings separately in the compiler. This patch is a base of implementing the built-ins, inputs and outputs of a geometry shader to ANGLE GLSL compiler. Unlike the vertex shaders (their outputs are varyings) and fragment shaders (their inputs are varyings), the inputs and outputs of geometry shaders are all varyings, so we need two vector<Varying> to store them correctly. BUG=angleproject:1941 Change-Id: I9e8cc16045d5e29e9a80a09dc31b33a7ae39b345 Reviewed-on: https://chromium-review.googlesource.com/593347 Commit-Queue: Geoff Lang <geofflang@chromium.org> Reviewed-by: 's avatarGeoff Lang <geofflang@chromium.org>
parent b0761934
......@@ -536,6 +536,8 @@ const std::map<std::string, std::string> *GetNameHashingMap(const ShHandle handl
// handle: Specifies the compiler
const std::vector<sh::Uniform> *GetUniforms(const ShHandle handle);
const std::vector<sh::Varying> *GetVaryings(const ShHandle handle);
const std::vector<sh::Varying> *GetInputVaryings(const ShHandle handle);
const std::vector<sh::Varying> *GetOutputVaryings(const ShHandle handle);
const std::vector<sh::Attribute> *GetAttributes(const ShHandle handle);
const std::vector<sh::OutputVariable> *GetOutputVariables(const ShHandle handle);
const std::vector<sh::InterfaceBlock> *GetInterfaceBlocks(const ShHandle handle);
......
......@@ -578,10 +578,11 @@ void PrintVariable(const std::string &prefix, size_t index, const sh::ShaderVari
static void PrintActiveVariables(ShHandle compiler)
{
const std::vector<sh::Uniform> *uniforms = sh::GetUniforms(compiler);
const std::vector<sh::Varying> *varyings = sh::GetVaryings(compiler);
const std::vector<sh::Varying> *inputVaryings = sh::GetInputVaryings(compiler);
const std::vector<sh::Varying> *outputVaryings = sh::GetOutputVaryings(compiler);
const std::vector<sh::Attribute> *attributes = sh::GetAttributes(compiler);
const std::vector<sh::OutputVariable> *outputs = sh::GetOutputVariables(compiler);
for (size_t varCategory = 0; varCategory < 4; ++varCategory)
for (size_t varCategory = 0; varCategory < 5; ++varCategory)
{
size_t numVars = 0;
std::string varCategoryName;
......@@ -592,11 +593,16 @@ static void PrintActiveVariables(ShHandle compiler)
}
else if (varCategory == 1)
{
numVars = varyings->size();
varCategoryName = "varying";
numVars = inputVaryings->size();
varCategoryName = "input varying";
}
else if (varCategory == 2)
{
numVars = outputVaryings->size();
varCategoryName = "output varying";
}
else if (varCategory == 3)
{
numVars = attributes->size();
varCategoryName = "attribute";
}
......@@ -612,8 +618,10 @@ static void PrintActiveVariables(ShHandle compiler)
if (varCategory == 0)
var = &((*uniforms)[i]);
else if (varCategory == 1)
var = &((*varyings)[i]);
var = &((*inputVaryings)[i]);
else if (varCategory == 2)
var = &((*outputVaryings)[i]);
else if (varCategory == 3)
var = &((*attributes)[i]);
else
var = &((*outputs)[i]);
......
......@@ -473,9 +473,9 @@ TIntermBlock *TCompiler::compileTreeImpl(const char *const shaderStrings[],
if (success && shouldCollectVariables(compileOptions))
{
ASSERT(!variablesCollected);
CollectVariables(root, &attributes, &outputVariables, &uniforms, &varyings,
&interfaceBlocks, hashFunction, &symbolTable, shaderVersion,
extensionBehavior);
CollectVariables(root, &attributes, &outputVariables, &uniforms, &inputVaryings,
&outputVaryings, &interfaceBlocks, hashFunction, &symbolTable,
shaderVersion, extensionBehavior);
variablesCollected = true;
if (compileOptions & SH_USE_UNUSED_STANDARD_SHARED_BLOCKS)
{
......@@ -760,7 +760,8 @@ void TCompiler::clearResults()
attributes.clear();
outputVariables.clear();
uniforms.clear();
varyings.clear();
inputVaryings.clear();
outputVaryings.clear();
interfaceBlocks.clear();
variablesCollected = false;
mGLPositionInitialized = false;
......@@ -999,7 +1000,7 @@ void TCompiler::initializeOutputVariables(TIntermBlock *root)
InitVariableList list;
if (shaderType == GL_VERTEX_SHADER)
{
for (auto var : varyings)
for (auto var : outputVaryings)
{
list.push_back(var);
if (var.name == "gl_Position")
......@@ -1063,9 +1064,16 @@ void TCompiler::writePragma(ShCompileOptions compileOptions)
bool TCompiler::isVaryingDefined(const char *varyingName)
{
ASSERT(variablesCollected);
for (size_t ii = 0; ii < varyings.size(); ++ii)
for (size_t ii = 0; ii < inputVaryings.size(); ++ii)
{
if (varyings[ii].name == varyingName)
if (inputVaryings[ii].name == varyingName)
{
return true;
}
}
for (size_t ii = 0; ii < outputVaryings.size(); ++ii)
{
if (outputVaryings[ii].name == varyingName)
{
return true;
}
......
......@@ -110,7 +110,8 @@ class TCompiler : public TShHandleBase
const std::vector<sh::Attribute> &getAttributes() const { return attributes; }
const std::vector<sh::OutputVariable> &getOutputVariables() const { return outputVariables; }
const std::vector<sh::Uniform> &getUniforms() const { return uniforms; }
const std::vector<sh::Varying> &getVaryings() const { return varyings; }
const std::vector<sh::Varying> &getInputVaryings() const { return inputVaryings; }
const std::vector<sh::Varying> &getOutputVaryings() const { return outputVaryings; }
const std::vector<sh::InterfaceBlock> &getInterfaceBlocks() const { return interfaceBlocks; }
ShHashFunction64 getHashFunction() const { return hashFunction; }
......@@ -184,7 +185,8 @@ class TCompiler : public TShHandleBase
std::vector<sh::Attribute> attributes;
std::vector<sh::OutputVariable> outputVariables;
std::vector<sh::Uniform> uniforms;
std::vector<sh::Varying> varyings;
std::vector<sh::Varying> inputVaryings;
std::vector<sh::Varying> outputVaryings;
std::vector<sh::InterfaceBlock> interfaceBlocks;
private:
......
......@@ -45,7 +45,20 @@ const std::vector<Uniform> *GetVariableList(const TCompiler *compiler)
template <>
const std::vector<Varying> *GetVariableList(const TCompiler *compiler)
{
return &compiler->getVaryings();
switch (compiler->getShaderType())
{
case GL_VERTEX_SHADER:
return &compiler->getOutputVaryings();
case GL_FRAGMENT_SHADER:
return &compiler->getInputVaryings();
case GL_COMPUTE_SHADER:
ASSERT(compiler->getOutputVaryings().empty() && compiler->getInputVaryings().empty());
return &compiler->getOutputVaryings();
// Since geometry shaders have both input and output varyings, we shouldn't call GetVaryings
// on a geometry shader.
default:
return nullptr;
}
}
template <>
......@@ -66,8 +79,7 @@ const std::vector<InterfaceBlock> *GetVariableList(const TCompiler *compiler)
return &compiler->getInterfaceBlocks();
}
template <typename VarT>
const std::vector<VarT> *GetShaderVariables(const ShHandle handle)
TCompiler *GetCompilerFromHandle(ShHandle handle)
{
if (!handle)
{
......@@ -75,7 +87,13 @@ const std::vector<VarT> *GetShaderVariables(const ShHandle handle)
}
TShHandleBase *base = static_cast<TShHandleBase *>(handle);
TCompiler *compiler = base->getAsCompiler();
return base->getAsCompiler();
}
template <typename VarT>
const std::vector<VarT> *GetShaderVariables(const ShHandle handle)
{
TCompiler *compiler = GetCompilerFromHandle(handle);
if (!compiler)
{
return nullptr;
......@@ -84,14 +102,6 @@ const std::vector<VarT> *GetShaderVariables(const ShHandle handle)
return GetVariableList<VarT>(compiler);
}
TCompiler *GetCompilerFromHandle(ShHandle handle)
{
if (!handle)
return nullptr;
TShHandleBase *base = static_cast<TShHandleBase *>(handle);
return base->getAsCompiler();
}
#ifdef ANGLE_ENABLE_HLSL
TranslatorHLSL *GetTranslatorHLSLFromHandle(ShHandle handle)
{
......@@ -355,6 +365,26 @@ const std::vector<Uniform> *GetUniforms(const ShHandle handle)
return GetShaderVariables<Uniform>(handle);
}
const std::vector<sh::Varying> *GetInputVaryings(const ShHandle handle)
{
TCompiler *compiler = GetCompilerFromHandle(handle);
if (compiler == nullptr)
{
return nullptr;
}
return &compiler->getInputVaryings();
}
const std::vector<sh::Varying> *GetOutputVaryings(const ShHandle handle)
{
TCompiler *compiler = GetCompilerFromHandle(handle);
if (compiler == nullptr)
{
return nullptr;
}
return &compiler->getOutputVaryings();
}
const std::vector<Varying> *GetVaryings(const ShHandle handle)
{
return GetShaderVariables<Varying>(handle);
......
......@@ -92,7 +92,8 @@ class CollectVariablesTraverser : public TIntermTraverser
CollectVariablesTraverser(std::vector<Attribute> *attribs,
std::vector<OutputVariable> *outputVariables,
std::vector<Uniform> *uniforms,
std::vector<Varying> *varyings,
std::vector<Varying> *inputVaryings,
std::vector<Varying> *outputVaryings,
std::vector<InterfaceBlock> *interfaceBlocks,
ShHashFunction64 hashFunction,
TSymbolTable *symbolTable,
......@@ -116,14 +117,17 @@ class CollectVariablesTraverser : public TIntermTraverser
void setBuiltInInfoFromSymbolTable(const char *name, ShaderVariable *info);
void recordBuiltInVaryingUsed(const char *name, bool *addedFlag);
void recordBuiltInVaryingUsed(const char *name,
bool *addedFlag,
std::vector<Varying> *varyings);
void recordBuiltInFragmentOutputUsed(const char *name, bool *addedFlag);
void recordBuiltInAttributeUsed(const char *name, bool *addedFlag);
std::vector<Attribute> *mAttribs;
std::vector<OutputVariable> *mOutputVariables;
std::vector<Uniform> *mUniforms;
std::vector<Varying> *mVaryings;
std::vector<Varying> *mInputVaryings;
std::vector<Varying> *mOutputVaryings;
std::vector<InterfaceBlock> *mInterfaceBlocks;
std::map<std::string, InterfaceBlockField *> mInterfaceBlockFields;
......@@ -155,7 +159,8 @@ CollectVariablesTraverser::CollectVariablesTraverser(
std::vector<sh::Attribute> *attribs,
std::vector<sh::OutputVariable> *outputVariables,
std::vector<sh::Uniform> *uniforms,
std::vector<sh::Varying> *varyings,
std::vector<sh::Varying> *inputVaryings,
std::vector<sh::Varying> *outputVaryings,
std::vector<sh::InterfaceBlock> *interfaceBlocks,
ShHashFunction64 hashFunction,
TSymbolTable *symbolTable,
......@@ -165,7 +170,8 @@ CollectVariablesTraverser::CollectVariablesTraverser(
mAttribs(attribs),
mOutputVariables(outputVariables),
mUniforms(uniforms),
mVaryings(varyings),
mInputVaryings(inputVaryings),
mOutputVaryings(outputVaryings),
mInterfaceBlocks(interfaceBlocks),
mDepthRangeAdded(false),
mPointCoordAdded(false),
......@@ -203,15 +209,18 @@ void CollectVariablesTraverser::setBuiltInInfoFromSymbolTable(const char *name,
info->precision = GLVariablePrecision(type);
}
void CollectVariablesTraverser::recordBuiltInVaryingUsed(const char *name, bool *addedFlag)
void CollectVariablesTraverser::recordBuiltInVaryingUsed(const char *name,
bool *addedFlag,
std::vector<Varying> *varyings)
{
ASSERT(varyings);
if (!(*addedFlag))
{
Varying info;
setBuiltInInfoFromSymbolTable(name, &info);
info.staticUse = true;
info.isInvariant = mSymbolTable->isVaryingInvariant(name);
mVaryings->push_back(info);
varyings->push_back(info);
(*addedFlag) = true;
}
}
......@@ -259,9 +268,13 @@ void CollectVariablesTraverser::visitSymbol(TIntermSymbol *symbol)
ShaderVariable *var = nullptr;
const TString &symbolName = symbol->getName().getString();
if (IsVarying(symbol->getQualifier()))
if (IsVaryingIn(symbol->getQualifier()))
{
var = FindVariable(symbolName, mVaryings);
var = FindVariable(symbolName, mInputVaryings);
}
else if (IsVaryingOut(symbol->getQualifier()))
{
var = FindVariable(symbolName, mOutputVaryings);
}
else if (symbol->getType().getBasicType() == EbtInterfaceBlock)
{
......@@ -351,13 +364,13 @@ void CollectVariablesTraverser::visitSymbol(TIntermSymbol *symbol)
}
break;
case EvqFragCoord:
recordBuiltInVaryingUsed("gl_FragCoord", &mFragCoordAdded);
recordBuiltInVaryingUsed("gl_FragCoord", &mFragCoordAdded, mInputVaryings);
return;
case EvqFrontFacing:
recordBuiltInVaryingUsed("gl_FrontFacing", &mFrontFacingAdded);
recordBuiltInVaryingUsed("gl_FrontFacing", &mFrontFacingAdded, mInputVaryings);
return;
case EvqPointCoord:
recordBuiltInVaryingUsed("gl_PointCoord", &mPointCoordAdded);
recordBuiltInVaryingUsed("gl_PointCoord", &mPointCoordAdded, mInputVaryings);
return;
case EvqInstanceID:
// Whenever the SH_INITIALIZE_BUILTINS_FOR_INSTANCED_MULTIVIEW option is set,
......@@ -384,13 +397,13 @@ void CollectVariablesTraverser::visitSymbol(TIntermSymbol *symbol)
recordBuiltInAttributeUsed("gl_VertexID", &mVertexIDAdded);
return;
case EvqPosition:
recordBuiltInVaryingUsed("gl_Position", &mPositionAdded);
recordBuiltInVaryingUsed("gl_Position", &mPositionAdded, mOutputVaryings);
return;
case EvqPointSize:
recordBuiltInVaryingUsed("gl_PointSize", &mPointSizeAdded);
recordBuiltInVaryingUsed("gl_PointSize", &mPointSizeAdded, mOutputVaryings);
return;
case EvqLastFragData:
recordBuiltInVaryingUsed("gl_LastFragData", &mLastFragDataAdded);
recordBuiltInVaryingUsed("gl_LastFragData", &mLastFragDataAdded, mInputVaryings);
return;
case EvqFragColor:
recordBuiltInFragmentOutputUsed("gl_FragColor", &mFragColorAdded);
......@@ -615,7 +628,15 @@ bool CollectVariablesTraverser::visitDeclaration(Visit, TIntermDeclaration *node
mUniforms->push_back(recordUniform(variable));
break;
default:
mVaryings->push_back(recordVarying(variable));
if (IsVaryingIn(qualifier))
{
mInputVaryings->push_back(recordVarying(variable));
}
else
{
ASSERT(IsVaryingOut(qualifier));
mOutputVaryings->push_back(recordVarying(variable));
}
break;
}
}
......@@ -660,16 +681,17 @@ void CollectVariables(TIntermBlock *root,
std::vector<Attribute> *attributes,
std::vector<OutputVariable> *outputVariables,
std::vector<Uniform> *uniforms,
std::vector<Varying> *varyings,
std::vector<Varying> *inputVaryings,
std::vector<Varying> *outputVaryings,
std::vector<InterfaceBlock> *interfaceBlocks,
ShHashFunction64 hashFunction,
TSymbolTable *symbolTable,
int shaderVersion,
const TExtensionBehavior &extensionBehavior)
{
CollectVariablesTraverser collect(attributes, outputVariables, uniforms, varyings,
interfaceBlocks, hashFunction, symbolTable, shaderVersion,
extensionBehavior);
CollectVariablesTraverser collect(attributes, outputVariables, uniforms, inputVaryings,
outputVaryings, interfaceBlocks, hashFunction, symbolTable,
shaderVersion, extensionBehavior);
root->traverse(&collect);
}
......
......@@ -21,7 +21,8 @@ void CollectVariables(TIntermBlock *root,
std::vector<Attribute> *attributes,
std::vector<OutputVariable> *outputVariables,
std::vector<Uniform> *uniforms,
std::vector<Varying> *varyings,
std::vector<Varying> *inputVaryings,
std::vector<Varying> *outputVaryings,
std::vector<InterfaceBlock> *interfaceBlocks,
ShHashFunction64 hashFunction,
TSymbolTable *symbolTable,
......
......@@ -421,7 +421,7 @@ TEST_F(CollectVertexVariablesTest, VaryingInterpolation)
compile(shaderString);
const std::vector<Varying> &varyings = mTranslator->getVaryings();
const std::vector<Varying> &varyings = mTranslator->getOutputVaryings();
ASSERT_EQ(2u, varyings.size());
const Varying *varying = &varyings[0];
......@@ -825,7 +825,7 @@ TEST_F(CollectVertexVariablesTest, ViewID_OVR)
SH_SELECT_VIEW_IN_NV_GLSL_VERTEX_SHADER);
// The internal ViewID_OVR varying is not exposed through the ShaderVars interface.
const auto &varyings = mTranslator->getVaryings();
const auto &varyings = mTranslator->getOutputVaryings();
ASSERT_EQ(1u, varyings.size());
const Varying *varying = &varyings[0];
EXPECT_EQ("gl_Position", varying->name);
......
......@@ -315,14 +315,14 @@ TEST(ShaderVariableTest, InvariantLeakAcrossShaders)
};
EXPECT_TRUE(sh::Compile(compiler, program1, 1, SH_VARIABLES));
const std::vector<sh::Varying> *varyings = sh::GetVaryings(compiler);
const std::vector<sh::Varying> *varyings = sh::GetOutputVaryings(compiler);
for (const sh::Varying &varying : *varyings)
{
if (varying.name == "v_varying")
EXPECT_TRUE(varying.isInvariant);
}
EXPECT_TRUE(sh::Compile(compiler, program2, 1, SH_VARIABLES));
varyings = sh::GetVaryings(compiler);
varyings = sh::GetOutputVaryings(compiler);
for (const sh::Varying &varying : *varyings)
{
if (varying.name == "v_varying")
......@@ -357,14 +357,14 @@ TEST(ShaderVariableTest, GlobalInvariantLeakAcrossShaders)
};
EXPECT_TRUE(sh::Compile(compiler, program1, 1, SH_VARIABLES));
const std::vector<sh::Varying> *varyings = sh::GetVaryings(compiler);
const std::vector<sh::Varying> *varyings = sh::GetOutputVaryings(compiler);
for (const sh::Varying &varying : *varyings)
{
if (varying.name == "v_varying")
EXPECT_TRUE(varying.isInvariant);
}
EXPECT_TRUE(sh::Compile(compiler, program2, 1, SH_VARIABLES));
varyings = sh::GetVaryings(compiler);
varyings = sh::GetOutputVaryings(compiler);
for (const sh::Varying &varying : *varyings)
{
if (varying.name == "v_varying")
......@@ -405,14 +405,14 @@ TEST(ShaderVariableTest, BuiltinInvariantVarying)
};
EXPECT_TRUE(sh::Compile(compiler, program1, 1, SH_VARIABLES));
const std::vector<sh::Varying> *varyings = sh::GetVaryings(compiler);
const std::vector<sh::Varying> *varyings = sh::GetOutputVaryings(compiler);
for (const sh::Varying &varying : *varyings)
{
if (varying.name == "gl_Position")
EXPECT_TRUE(varying.isInvariant);
}
EXPECT_TRUE(sh::Compile(compiler, program2, 1, SH_VARIABLES));
varyings = sh::GetVaryings(compiler);
varyings = sh::GetOutputVaryings(compiler);
for (const sh::Varying &varying : *varyings)
{
if (varying.name == "gl_Position")
......
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