Refactor ShaderVariables to store fields in the base.
Instead of only storing structure information in Varyings, Uniforms
and Interface Block Fields, store it in the base class. Also only
store base variable information for struct fields, instead of fully
typed information. This works because stuff like interpolation type,
invariance, and other properties are for the entire variable, not
individual fields.
Also add new fields for interface block instance name, varying
invariance and structure name for all struct types.
BUG=angle:466
Change-Id: If03fc071e6becb7aad6dea5093989bba7daee69e
Reviewed-on: https://chromium-review.googlesource.com/213501
Reviewed-by: Zhenyao Mo <zmo@chromium.org>
Tested-by: Jamie Madill <jmadill@chromium.org>
Reviewed-by: Nicolas Capens <capn@chromium.org>
diff --git a/src/compiler/translator/Compiler.h b/src/compiler/translator/Compiler.h
index 5eac2d8..6bef5c1 100644
--- a/src/compiler/translator/Compiler.h
+++ b/src/compiler/translator/Compiler.h
@@ -71,9 +71,9 @@
const std::vector<sh::Attribute> &getAttributes() const { return attributes; }
const std::vector<sh::Attribute> &getOutputVariables() const { return outputVariables; }
const std::vector<sh::Uniform> &getUniforms() const { return uniforms; }
- const std::vector<sh::Uniform> &getExpandedUniforms() const { return expandedUniforms; }
+ const std::vector<sh::ShaderVariable> &getExpandedUniforms() const { return expandedUniforms; }
const std::vector<sh::Varying> &getVaryings() const { return varyings; }
- const std::vector<sh::Varying> &getExpandedVaryings() const { return expandedVaryings; }
+ const std::vector<sh::ShaderVariable> &getExpandedVaryings() const { return expandedVaryings; }
const std::vector<sh::InterfaceBlock> &getInterfaceBlocks() const { return interfaceBlocks; }
ShHashFunction64 getHashFunction() const { return hashFunction; }
@@ -138,9 +138,9 @@
std::vector<sh::Attribute> attributes;
std::vector<sh::Attribute> outputVariables;
std::vector<sh::Uniform> uniforms;
- std::vector<sh::Uniform> expandedUniforms;
+ std::vector<sh::ShaderVariable> expandedUniforms;
std::vector<sh::Varying> varyings;
- std::vector<sh::Varying> expandedVaryings;
+ std::vector<sh::ShaderVariable> expandedVaryings;
std::vector<sh::InterfaceBlock> interfaceBlocks;
private:
diff --git a/src/compiler/translator/OutputHLSL.cpp b/src/compiler/translator/OutputHLSL.cpp
index e0a90ec..defb34a 100644
--- a/src/compiler/translator/OutputHLSL.cpp
+++ b/src/compiler/translator/OutputHLSL.cpp
@@ -2922,29 +2922,12 @@
return constUnion;
}
-class DeclareVaryingTraverser : public GetVariableTraverser<Varying>
-{
- public:
- DeclareVaryingTraverser(std::vector<Varying> *output,
- InterpolationType interpolation)
- : GetVariableTraverser(output),
- mInterpolation(interpolation)
- {}
-
- private:
- void visitVariable(Varying *varying)
- {
- varying->interpolation = mInterpolation;
- }
-
- InterpolationType mInterpolation;
-};
-
void OutputHLSL::declareVaryingToList(const TType &type, TQualifier baseTypeQualifier,
const TString &name, std::vector<Varying> &fieldsOut)
{
- DeclareVaryingTraverser traverser(&fieldsOut, GetInterpolationType(baseTypeQualifier));
- traverser.traverse(type, name);
+ GetVariableTraverser traverser;
+ traverser.traverse(type, name, &fieldsOut);
+ fieldsOut.back().interpolation = GetInterpolationType(baseTypeQualifier);
}
}
diff --git a/src/compiler/translator/ShaderVars.cpp b/src/compiler/translator/ShaderVars.cpp
index 164eaa6..90f3bd0 100644
--- a/src/compiler/translator/ShaderVars.cpp
+++ b/src/compiler/translator/ShaderVars.cpp
@@ -35,7 +35,9 @@
name(other.name),
mappedName(other.mappedName),
arraySize(other.arraySize),
- staticUse(other.staticUse)
+ staticUse(other.staticUse),
+ fields(other.fields),
+ structName(other.structName)
{}
ShaderVariable &ShaderVariable::operator=(const ShaderVariable &other)
@@ -46,6 +48,8 @@
mappedName = other.mappedName;
arraySize = other.arraySize;
staticUse = other.staticUse;
+ fields = other.fields;
+ structName = other.structName;
return *this;
}
@@ -56,14 +60,12 @@
{}
Uniform::Uniform(const Uniform &other)
- : ShaderVariable(other),
- fields(other.fields)
+ : ShaderVariable(other)
{}
Uniform &Uniform::operator=(const Uniform &other)
{
ShaderVariable::operator=(other);
- fields = other.fields;
return *this;
}
@@ -95,20 +97,19 @@
InterfaceBlockField::InterfaceBlockField(const InterfaceBlockField &other)
: ShaderVariable(other),
- isRowMajorMatrix(other.isRowMajorMatrix),
- fields(other.fields)
+ isRowMajorMatrix(other.isRowMajorMatrix)
{}
InterfaceBlockField &InterfaceBlockField::operator=(const InterfaceBlockField &other)
{
ShaderVariable::operator=(other);
isRowMajorMatrix = other.isRowMajorMatrix;
- fields = other.fields;
return *this;
}
Varying::Varying()
- : interpolation(INTERPOLATION_SMOOTH)
+ : interpolation(INTERPOLATION_SMOOTH),
+ isInvariant(false)
{}
Varying::~Varying()
@@ -117,16 +118,14 @@
Varying::Varying(const Varying &other)
: ShaderVariable(other),
interpolation(other.interpolation),
- fields(other.fields),
- structName(other.structName)
+ isInvariant(other.isInvariant)
{}
Varying &Varying::operator=(const Varying &other)
{
ShaderVariable::operator=(other);
interpolation = other.interpolation;
- fields = other.fields;
- structName = other.structName;
+ isInvariant = other.isInvariant;
return *this;
}
@@ -143,6 +142,7 @@
InterfaceBlock::InterfaceBlock(const InterfaceBlock &other)
: name(other.name),
mappedName(other.mappedName),
+ instanceName(other.instanceName),
arraySize(other.arraySize),
layout(other.layout),
isRowMajorLayout(other.isRowMajorLayout),
@@ -154,6 +154,7 @@
{
name = other.name;
mappedName = other.mappedName;
+ instanceName = other.instanceName;
arraySize = other.arraySize;
layout = other.layout;
isRowMajorLayout = other.isRowMajorLayout;
diff --git a/src/compiler/translator/UniformHLSL.cpp b/src/compiler/translator/UniformHLSL.cpp
index e24da6a..2c99a90 100644
--- a/src/compiler/translator/UniformHLSL.cpp
+++ b/src/compiler/translator/UniformHLSL.cpp
@@ -30,18 +30,6 @@
}
}
-static TString InterfaceBlockFieldName(const TInterfaceBlock &interfaceBlock, const TField &field)
-{
- if (interfaceBlock.hasInstanceName())
- {
- return interfaceBlock.name() + "." + field.name();
- }
- else
- {
- return field.name();
- }
-}
-
static TString InterfaceBlockFieldTypeString(const TField &field, TLayoutBlockStorage blockStorage)
{
const TType &fieldType = *field.type();
@@ -94,8 +82,8 @@
{
unsigned int registerIndex = (IsSampler(type.getBasicType()) ? mSamplerRegister : mUniformRegister);
- GetVariableTraverser<Uniform> traverser(&mActiveUniforms);
- traverser.traverse(type, name);
+ GetVariableTraverser traverser;
+ traverser.traverse(type, name, &mActiveUniforms);
const sh::Uniform &activeUniform = mActiveUniforms.back();
mUniformRegisterMap[activeUniform.name] = registerIndex;
@@ -162,7 +150,6 @@
{
const TType &nodeType = interfaceBlockIt->second->getType();
const TInterfaceBlock &interfaceBlock = *nodeType.getInterfaceBlock();
- const TFieldList &fieldList = interfaceBlock.fields();
unsigned int arraySize = static_cast<unsigned int>(interfaceBlock.arraySize());
unsigned int activeRegister = mInterfaceBlockRegister;
@@ -171,15 +158,7 @@
activeBlock.name = interfaceBlock.name().c_str();
activeBlock.arraySize = arraySize;
- for (unsigned int typeIndex = 0; typeIndex < fieldList.size(); typeIndex++)
- {
- const TField &field = *fieldList[typeIndex];
- const TString &fullFieldName = InterfaceBlockFieldName(interfaceBlock, field);
-
- bool isRowMajor = (field.type()->getLayoutQualifier().matrixPacking == EmpRowMajor);
- GetInterfaceBlockFieldTraverser traverser(&activeBlock.fields, isRowMajor);
- traverser.traverse(*field.type(), fullFieldName);
- }
+ GetInterfaceBlockFields(interfaceBlock, &activeBlock.fields);
mInterfaceBlockRegisterMap[activeBlock.name] = activeRegister;
mInterfaceBlockRegister += std::max(1u, arraySize);
diff --git a/src/compiler/translator/VariableInfo.cpp b/src/compiler/translator/VariableInfo.cpp
index 54129d4..b331581 100644
--- a/src/compiler/translator/VariableInfo.cpp
+++ b/src/compiler/translator/VariableInfo.cpp
@@ -9,20 +9,17 @@
#include "compiler/translator/util.h"
#include "common/utilities.h"
-template <typename VarT>
-static void ExpandUserDefinedVariable(const VarT &variable,
+static void ExpandUserDefinedVariable(const sh::ShaderVariable &variable,
const std::string &name,
const std::string &mappedName,
bool markStaticUse,
- std::vector<VarT> *expanded);
+ std::vector<sh::ShaderVariable> *expanded);
-// Returns info for an attribute, uniform, or varying.
-template <typename VarT>
-static void ExpandVariable(const VarT &variable,
+static void ExpandVariable(const sh::ShaderVariable &variable,
const std::string &name,
const std::string &mappedName,
bool markStaticUse,
- std::vector<VarT> *expanded)
+ std::vector<sh::ShaderVariable> *expanded)
{
if (variable.isStruct())
{
@@ -42,7 +39,7 @@
}
else
{
- VarT expandedVar = variable;
+ sh::ShaderVariable expandedVar = variable;
expandedVar.name = name;
expandedVar.mappedName = mappedName;
@@ -63,20 +60,19 @@
}
}
-template <class VarT>
-static void ExpandUserDefinedVariable(const VarT &variable,
+static void ExpandUserDefinedVariable(const sh::ShaderVariable &variable,
const std::string &name,
const std::string &mappedName,
bool markStaticUse,
- std::vector<VarT> *expanded)
+ std::vector<sh::ShaderVariable> *expanded)
{
ASSERT(variable.isStruct());
- const std::vector<VarT> &fields = variable.fields;
+ const std::vector<sh::ShaderVariable> &fields = variable.fields;
for (size_t fieldIndex = 0; fieldIndex < fields.size(); fieldIndex++)
{
- const VarT &field = fields[fieldIndex];
+ const sh::ShaderVariable &field = fields[fieldIndex];
ExpandVariable(field,
name + "." + field.name,
mappedName + "." + field.mappedName,
@@ -216,17 +212,17 @@
}
}
-template <typename VarT>
-class NameHashingTraverser : public sh::GetVariableTraverser<VarT>
+class NameHashingTraverser : public sh::GetVariableTraverser
{
public:
- NameHashingTraverser(std::vector<VarT> *output, ShHashFunction64 hashFunction)
- : sh::GetVariableTraverser<VarT>(output),
- mHashFunction(hashFunction)
+ NameHashingTraverser(ShHashFunction64 hashFunction)
+ : mHashFunction(hashFunction)
{}
private:
- void visitVariable(VarT *variable)
+ DISALLOW_COPY_AND_ASSIGN(NameHashingTraverser);
+
+ virtual void visitVariable(sh::ShaderVariable *variable)
{
TString stringName = TString(variable->name.c_str());
variable->mappedName = TIntermTraverser::hash(stringName, mHashFunction).c_str();
@@ -262,26 +258,16 @@
{
sh::InterfaceBlock interfaceBlock;
const TInterfaceBlock *blockType = variable->getType().getInterfaceBlock();
-
- bool isRowMajor = (blockType->matrixPacking() == EmpRowMajor);
+ ASSERT(blockType);
interfaceBlock.name = blockType->name().c_str();
interfaceBlock.mappedName = TIntermTraverser::hash(variable->getSymbol(), mHashFunction).c_str();
+ interfaceBlock.instanceName = (blockType->hasInstanceName() ? blockType->instanceName().c_str() : "");
interfaceBlock.arraySize = variable->getArraySize();
- interfaceBlock.isRowMajorLayout = isRowMajor;
+ interfaceBlock.isRowMajorLayout = (blockType->matrixPacking() == EmpRowMajor);
interfaceBlock.layout = sh::GetBlockLayoutType(blockType->blockStorage());
- ASSERT(blockType);
- const TFieldList &blockFields = blockType->fields();
-
- for (size_t fieldIndex = 0; fieldIndex < blockFields.size(); fieldIndex++)
- {
- const TField *field = blockFields[fieldIndex];
- ASSERT(field);
-
- sh::GetInterfaceBlockFieldTraverser traverser(&interfaceBlock.fields, isRowMajor);
- traverser.traverse(*field->type(), field->name());
- }
+ sh::GetInterfaceBlockFields(*blockType, &interfaceBlock.fields);
infoList->push_back(interfaceBlock);
}
@@ -290,8 +276,8 @@
void CollectVariables::visitVariable(const TIntermSymbol *variable,
std::vector<VarT> *infoList) const
{
- NameHashingTraverser<VarT> traverser(infoList, mHashFunction);
- traverser.traverse(variable->getType(), variable->getSymbol());
+ NameHashingTraverser traverser(mHashFunction);
+ traverser.traverse(variable->getType(), variable->getSymbol(), infoList);
}
template <typename VarT>
@@ -361,14 +347,15 @@
}
template <typename VarT>
-void ExpandVariables(const std::vector<VarT> &compact, std::vector<VarT> *expanded)
+void ExpandVariables(const std::vector<VarT> &compact,
+ std::vector<sh::ShaderVariable> *expanded)
{
for (size_t variableIndex = 0; variableIndex < compact.size(); variableIndex++)
{
- const VarT &variable = compact[variableIndex];
+ const sh::ShaderVariable &variable = compact[variableIndex];
ExpandVariable(variable, variable.name, variable.mappedName, variable.staticUse, expanded);
}
}
-template void ExpandVariables(const std::vector<sh::Uniform> &, std::vector<sh::Uniform> *);
-template void ExpandVariables(const std::vector<sh::Varying> &, std::vector<sh::Varying> *);
+template void ExpandVariables(const std::vector<sh::Uniform> &, std::vector<sh::ShaderVariable> *);
+template void ExpandVariables(const std::vector<sh::Varying> &, std::vector<sh::ShaderVariable> *);
diff --git a/src/compiler/translator/VariableInfo.h b/src/compiler/translator/VariableInfo.h
index 03f7d62..d39bb21 100644
--- a/src/compiler/translator/VariableInfo.h
+++ b/src/compiler/translator/VariableInfo.h
@@ -48,8 +48,8 @@
};
// Expand struct variables to flattened lists of split variables
-// Implemented for sh::Varying and sh::Uniform.
template <typename VarT>
-void ExpandVariables(const std::vector<VarT> &compact, std::vector<VarT> *expanded);
+void ExpandVariables(const std::vector<VarT> &compact,
+ std::vector<sh::ShaderVariable> *expanded);
#endif // COMPILER_VARIABLE_INFO_H_
diff --git a/src/compiler/translator/util.cpp b/src/compiler/translator/util.cpp
index 46ce99c..8684a13 100644
--- a/src/compiler/translator/util.cpp
+++ b/src/compiler/translator/util.cpp
@@ -282,7 +282,7 @@
}
template <typename VarT>
-void GetVariableTraverser<VarT>::traverse(const TType &type, const TString &name)
+void GetVariableTraverser::traverse(const TType &type, const TString &name, std::vector<VarT> *output)
{
const TStructure *structure = type.getStruct();
@@ -297,51 +297,28 @@
}
else
{
+ // Note: this enum value is not exposed outside ANGLE
variable.type = GL_STRUCT_ANGLEX;
-
- mOutputStack.push(&variable.fields);
+ variable.structName = structure->name().c_str();
const TFieldList &fields = structure->fields();
for (size_t fieldIndex = 0; fieldIndex < fields.size(); fieldIndex++)
{
TField *field = fields[fieldIndex];
- traverse(*field->type(), field->name());
+ traverse(*field->type(), field->name(), &variable.fields);
}
-
- mOutputStack.pop();
}
visitVariable(&variable);
- ASSERT(!mOutputStack.empty());
- mOutputStack.top()->push_back(variable);
-}
-
-template <typename VarT>
-GetVariableTraverser<VarT>::GetVariableTraverser(std::vector<VarT> *output)
-{
ASSERT(output);
- mOutputStack.push(output);
+ output->push_back(variable);
}
-template class GetVariableTraverser<Uniform>;
-template class GetVariableTraverser<Varying>;
-template class GetVariableTraverser<InterfaceBlockField>;
-
-GetInterfaceBlockFieldTraverser::GetInterfaceBlockFieldTraverser(std::vector<InterfaceBlockField> *output, bool isRowMajorMatrix)
- : GetVariableTraverser(output),
- mIsRowMajorMatrix(isRowMajorMatrix)
-{
-}
-
-void GetInterfaceBlockFieldTraverser::visitVariable(InterfaceBlockField *newField)
-{
- if (gl::IsMatrixType(newField->type))
- {
- newField->isRowMajorMatrix = mIsRowMajorMatrix;
- }
-}
+template void GetVariableTraverser::traverse(const TType &, const TString &, std::vector<Uniform> *);
+template void GetVariableTraverser::traverse(const TType &, const TString &, std::vector<Varying> *);
+template void GetVariableTraverser::traverse(const TType &, const TString &, std::vector<InterfaceBlockField> *);
BlockLayoutType GetBlockLayoutType(TLayoutBlockStorage blockStorage)
{
@@ -354,4 +331,33 @@
}
}
+static TString InterfaceBlockFieldName(const TInterfaceBlock &interfaceBlock, const TField &field)
+{
+ if (interfaceBlock.hasInstanceName())
+ {
+ return interfaceBlock.name() + "." + field.name();
+ }
+ else
+ {
+ return field.name();
+ }
+}
+
+void GetInterfaceBlockFields(const TInterfaceBlock &interfaceBlock, std::vector<InterfaceBlockField> *fieldsOut)
+{
+ const TFieldList &fieldList = interfaceBlock.fields();
+
+ for (size_t fieldIndex = 0; fieldIndex < fieldList.size(); ++fieldIndex)
+ {
+ const TField &field = *fieldList[fieldIndex];
+ const TString &fullFieldName = InterfaceBlockFieldName(interfaceBlock, field);
+ const TType &fieldType = *field.type();
+
+ GetVariableTraverser traverser;
+ traverser.traverse(fieldType, fullFieldName, fieldsOut);
+
+ fieldsOut->back().isRowMajorMatrix = (fieldType.getLayoutQualifier().matrixPacking == EmpRowMajor);
+ }
+}
+
}
diff --git a/src/compiler/translator/util.h b/src/compiler/translator/util.h
index 2c16a1d..6a39d56 100644
--- a/src/compiler/translator/util.h
+++ b/src/compiler/translator/util.h
@@ -36,31 +36,23 @@
BlockLayoutType GetBlockLayoutType(TLayoutBlockStorage blockStorage);
TString ArrayString(const TType &type);
-template <typename VarT>
class GetVariableTraverser
{
public:
- GetVariableTraverser(std::vector<VarT> *output);
- void traverse(const TType &type, const TString &name);
+ GetVariableTraverser() {}
+
+ template <typename VarT>
+ void traverse(const TType &type, const TString &name, std::vector<VarT> *output);
protected:
// May be overloaded
- virtual void visitVariable(VarT *newVar) {}
+ virtual void visitVariable(ShaderVariable *newVar) {}
private:
- std::stack<std::vector<VarT> *> mOutputStack;
+ DISALLOW_COPY_AND_ASSIGN(GetVariableTraverser);
};
-struct GetInterfaceBlockFieldTraverser : public GetVariableTraverser<InterfaceBlockField>
-{
- public:
- GetInterfaceBlockFieldTraverser(std::vector<InterfaceBlockField> *output, bool isRowMajorMatrix);
-
- private:
- virtual void visitVariable(InterfaceBlockField *newField);
-
- bool mIsRowMajorMatrix;
-};
+void GetInterfaceBlockFields(const TInterfaceBlock &interfaceBlock, std::vector<InterfaceBlockField> *fieldsOut);
}