Commit 97fa8557 by Olli Etuaho Committed by Commit Bot

Refactor TStructure

Move methods and member variables that generalize to different types of field lists into TFieldListCollection and put implementations of methods in cpp files. This prepares for making TStructure inherit from TSymbol. BUG=angleproject:2267 TEST=angle_unittests Change-Id: I63095242dd17aac2d2efd616b49be1143cfc1f92 Reviewed-on: https://chromium-review.googlesource.com/793813Reviewed-by: 's avatarJamie Madill <jmadill@chromium.org> Reviewed-by: 's avatarCorentin Wallez <cwallez@chromium.org> Commit-Queue: Olli Etuaho <oetuaho@nvidia.com>
parent 18841310
......@@ -312,7 +312,7 @@ void TOutputGLSLBase::writeVariableType(const TType &type)
if (!structure->name().empty())
{
mDeclaredStructs.insert(structure->uniqueId());
mDeclaredStructs.insert(structure->uniqueId().get());
}
}
else if (type.getBasicType() == EbtInterfaceBlock)
......@@ -1168,7 +1168,7 @@ bool TOutputGLSLBase::structDeclared(const TStructure *structure) const
return false;
}
return (mDeclaredStructs.count(structure->uniqueId()) > 0);
return (mDeclaredStructs.count(structure->uniqueId().get()) > 0);
}
void TOutputGLSLBase::declareStruct(const TStructure *structure)
......
......@@ -25,7 +25,7 @@ void RegenerateStructNames::visitSymbol(TIntermSymbol *symbol)
return;
}
int uniqueId = userType->uniqueId();
int uniqueId = userType->uniqueId().get();
ASSERT(mScopeDepth > 0);
if (mScopeDepth == 1)
......
......@@ -72,10 +72,10 @@ void CollectVariableRefCountsTraverser::incrementStructTypeRefCount(const TType
const auto *structure = type.getStruct();
if (structure != nullptr)
{
auto structIter = mStructIdRefCounts.find(structure->uniqueId());
auto structIter = mStructIdRefCounts.find(structure->uniqueId().get());
if (structIter == mStructIdRefCounts.end())
{
mStructIdRefCounts[structure->uniqueId()] = 1u;
mStructIdRefCounts[structure->uniqueId().get()] = 1u;
for (const auto &field : structure->fields())
{
......@@ -158,8 +158,8 @@ void RemoveUnreferencedVariablesTraverser::decrementStructTypeRefCount(const TTy
auto *structure = type.getStruct();
if (structure != nullptr)
{
ASSERT(mStructIdRefCounts->find(structure->uniqueId()) != mStructIdRefCounts->end());
unsigned int structRefCount = --(*mStructIdRefCounts)[structure->uniqueId()];
ASSERT(mStructIdRefCounts->find(structure->uniqueId().get()) != mStructIdRefCounts->end());
unsigned int structRefCount = --(*mStructIdRefCounts)[structure->uniqueId().get()];
if (structRefCount == 0)
{
......@@ -176,7 +176,7 @@ void RemoveUnreferencedVariablesTraverser::removeVariableDeclaration(TIntermDecl
{
if (declarator->getType().isStructSpecifier() && !declarator->getType().isNamelessStruct())
{
unsigned int structId = declarator->getType().getStruct()->uniqueId();
unsigned int structId = declarator->getType().getStruct()->uniqueId().get();
if ((*mStructIdRefCounts)[structId] > 1u)
{
// If this declaration declares a named struct type that is used elsewhere, we need to
......
......@@ -422,6 +422,31 @@ TString TType::getCompleteString() const
return stream.str();
}
int TType::getDeepestStructNesting() const
{
return mStructure ? mStructure->deepestNesting() : 0;
}
bool TType::isNamelessStruct() const
{
return mStructure && mStructure->name() == "";
}
bool TType::isStructureContainingArrays() const
{
return mStructure ? mStructure->containsArrays() : false;
}
bool TType::isStructureContainingType(TBasicType t) const
{
return mStructure ? mStructure->containsType(t) : false;
}
bool TType::isStructureContainingSamplers() const
{
return mStructure ? mStructure->containsSamplers() : false;
}
//
// Recursively generate mangled names.
//
......@@ -553,10 +578,14 @@ const char *TType::buildMangledName() const
mangledName += "ac";
break;
case EbtStruct:
mangledName += mStructure->mangledName();
mangledName += "struct-";
mangledName += mStructure->name();
mangledName += mStructure->mangledFieldList();
break;
case EbtInterfaceBlock:
mangledName += mInterfaceBlock->mangledName();
mangledName += "iblock-";
mangledName += mInterfaceBlock->name();
mangledName += mInterfaceBlock->mangledFieldList();
break;
default:
// EbtVoid, EbtAddress and non types
......@@ -848,44 +877,38 @@ void TType::invalidateMangledName()
// TStructure implementation.
TStructure::TStructure(TSymbolTable *symbolTable, const TString *name, TFieldList *fields)
: TFieldListCollection(name, fields),
mDeepestNesting(0),
mUniqueId(symbolTable->nextUniqueId()),
mAtGlobalScope(false)
{
}
bool TStructure::equals(const TStructure &other) const
{
return (uniqueId() == other.uniqueId());
}
bool TStructure::containsArrays() const
bool TFieldListCollection::containsArrays() const
{
for (size_t i = 0; i < mFields->size(); ++i)
for (const auto *field : *mFields)
{
const TType *fieldType = (*mFields)[i]->type();
const TType *fieldType = field->type();
if (fieldType->isArray() || fieldType->isStructureContainingArrays())
return true;
}
return false;
}
bool TStructure::containsType(TBasicType type) const
bool TFieldListCollection::containsType(TBasicType type) const
{
for (size_t i = 0; i < mFields->size(); ++i)
for (const auto *field : *mFields)
{
const TType *fieldType = (*mFields)[i]->type();
const TType *fieldType = field->type();
if (fieldType->getBasicType() == type || fieldType->isStructureContainingType(type))
return true;
}
return false;
}
bool TStructure::containsSamplers() const
bool TFieldListCollection::containsSamplers() const
{
for (size_t i = 0; i < mFields->size(); ++i)
for (const auto *field : *mFields)
{
const TType *fieldType = (*mFields)[i]->type();
const TType *fieldType = field->type();
if (IsSampler(fieldType->getBasicType()) || fieldType->isStructureContainingSamplers())
return true;
}
......@@ -952,10 +975,9 @@ void TStructure::createSamplerSymbols(const TString &namePrefix,
}
}
TString TFieldListCollection::buildMangledName(const TString &mangledNamePrefix) const
TString TFieldListCollection::buildMangledFieldList() const
{
TString mangledName(mangledNamePrefix);
mangledName += *mName;
TString mangledName;
for (size_t i = 0; i < mFields->size(); ++i)
{
mangledName += '-';
......@@ -978,6 +1000,13 @@ size_t TFieldListCollection::calculateObjectSize() const
return size;
}
size_t TFieldListCollection::objectSize() const
{
if (mObjectSize == 0)
mObjectSize = calculateObjectSize();
return mObjectSize;
}
int TFieldListCollection::getLocationCount() const
{
int count = 0;
......@@ -996,7 +1025,21 @@ int TFieldListCollection::getLocationCount() const
return count;
}
int TStructure::calculateDeepestNesting() const
int TFieldListCollection::deepestNesting() const
{
if (mDeepestNesting == 0)
mDeepestNesting = calculateDeepestNesting();
return mDeepestNesting;
}
const TString &TFieldListCollection::mangledFieldList() const
{
if (mMangledFieldList.empty())
mMangledFieldList = buildMangledFieldList();
return mMangledFieldList;
}
int TFieldListCollection::calculateDeepestNesting() const
{
int maxNesting = 0;
for (size_t i = 0; i < mFields->size(); ++i)
......
......@@ -59,29 +59,34 @@ class TFieldListCollection : angle::NonCopyable
const TString &name() const { return *mName; }
const TFieldList &fields() const { return *mFields; }
size_t objectSize() const
{
if (mObjectSize == 0)
mObjectSize = calculateObjectSize();
return mObjectSize;
}
bool containsArrays() const;
bool containsType(TBasicType t) const;
bool containsSamplers() const;
size_t objectSize() const;
// How many locations the field list consumes as a uniform.
int getLocationCount() const;
int deepestNesting() const;
const TString &mangledFieldList() const;
protected:
TFieldListCollection(const TString *name, TFieldList *fields)
: mName(name), mFields(fields), mObjectSize(0)
: mName(name), mFields(fields), mObjectSize(0), mDeepestNesting(0)
{
}
TString buildMangledName(const TString &mangledNamePrefix) const;
size_t calculateObjectSize() const;
const TString *mName;
TFieldList *mFields;
mutable TString mMangledName;
private:
size_t calculateObjectSize() const;
int calculateDeepestNesting() const;
TString buildMangledFieldList() const;
mutable size_t mObjectSize;
mutable int mDeepestNesting;
mutable TString mMangledFieldList;
};
// May also represent interface blocks
......@@ -91,37 +96,17 @@ class TStructure : public TFieldListCollection
POOL_ALLOCATOR_NEW_DELETE();
TStructure(TSymbolTable *symbolTable, const TString *name, TFieldList *fields);
int deepestNesting() const
{
if (mDeepestNesting == 0)
mDeepestNesting = calculateDeepestNesting();
return mDeepestNesting;
}
bool containsArrays() const;
bool containsType(TBasicType t) const;
bool containsSamplers() const;
void createSamplerSymbols(const TString &namePrefix,
const TString &apiNamePrefix,
TVector<TIntermSymbol *> *outputSymbols,
TMap<TIntermSymbol *, TString> *outputSymbolsToAPINames,
TSymbolTable *symbolTable) const;
bool equals(const TStructure &other) const;
int uniqueId() const { return mUniqueId.get(); }
const TSymbolUniqueId &uniqueId() const { return mUniqueId; }
void setAtGlobalScope(bool atGlobalScope) { mAtGlobalScope = atGlobalScope; }
bool atGlobalScope() const { return mAtGlobalScope; }
const TString &mangledName() const
{
if (mMangledName.empty())
mMangledName = buildMangledName("struct-");
return mMangledName;
}
private:
// TODO(zmo): Find a way to get rid of the const_cast in function
// setName(). At the moment keep this function private so only
......@@ -133,9 +118,6 @@ class TStructure : public TFieldListCollection
*mutableName = name;
}
int calculateDeepestNesting() const;
mutable int mDeepestNesting;
const TSymbolUniqueId mUniqueId;
bool mAtGlobalScope;
};
......@@ -161,12 +143,6 @@ class TInterfaceBlock : public TFieldListCollection
TLayoutBlockStorage blockStorage() const { return mBlockStorage; }
TLayoutMatrixPacking matrixPacking() const { return mMatrixPacking; }
int blockBinding() const { return mBinding; }
const TString &mangledName() const
{
if (mMangledName.empty())
mMangledName = buildMangledName("iblock-");
return mMangledName;
}
private:
const TString *mInstanceName; // for interface block instance names
......@@ -348,24 +324,13 @@ class TType
// For type "nesting2", this method would return 2 -- the number
// of structures through which indirection must occur to reach the
// deepest field (nesting2.field1.position).
int getDeepestStructNesting() const { return mStructure ? mStructure->deepestNesting() : 0; }
int getDeepestStructNesting() const;
bool isNamelessStruct() const { return mStructure && mStructure->name() == ""; }
bool isNamelessStruct() const;
bool isStructureContainingArrays() const
{
return mStructure ? mStructure->containsArrays() : false;
}
bool isStructureContainingType(TBasicType t) const
{
return mStructure ? mStructure->containsType(t) : false;
}
bool isStructureContainingSamplers() const
{
return mStructure ? mStructure->containsSamplers() : false;
}
bool isStructureContainingArrays() const;
bool isStructureContainingType(TBasicType t) const;
bool isStructureContainingSamplers() const;
bool isStructSpecifier() const { return mIsStructSpecifier; }
......
......@@ -859,7 +859,7 @@ TString StructNameString(const TStructure &structure)
return Decorate(structure.name());
}
return "ss" + str(structure.uniqueId()) + "_" + structure.name();
return "ss" + str(structure.uniqueId().get()) + "_" + structure.name();
}
TString QualifiedStructNameString(const TStructure &structure,
......
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