Skip to content

Commit

Permalink
ES31: Use std::map<const TField *, BlockMemberInfo> to save ssbo info
Browse files Browse the repository at this point in the history
Bug: angleproject:1951
Change-Id: I9d6a19c0d63065db69985845a1cd68cfd4b99d1f
Reviewed-on: https://chromium-review.googlesource.com/c/1298913
Commit-Queue: Jiajia Qin <jiajia.qin@intel.com>
Reviewed-by: Geoff Lang <geofflang@chromium.org>
Reviewed-by: Jamie Madill <jmadill@chromium.org>
Reviewed-by: Corentin Wallez <cwallez@chromium.org>
  • Loading branch information
qjia7 authored and Commit Bot committed Oct 29, 2018
1 parent bfe31c4 commit df73a8e
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 37 deletions.
54 changes: 29 additions & 25 deletions src/compiler/translator/ShaderStorageBlockOutputHLSL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
#include "compiler/translator/ShaderStorageBlockOutputHLSL.h"

#include "compiler/translator/ResourcesHLSL.h"
#include "compiler/translator/blocklayout.h"
#include "compiler/translator/blocklayoutHLSL.h"
#include "compiler/translator/util.h"

Expand All @@ -49,12 +48,14 @@ const TField *GetFieldMemberInShaderStorageBlock(const TInterfaceBlock *interfac
return nullptr;
}

void SetShaderStorageBlockFieldMemberInfo(const TFieldList &fields,
void GetShaderStorageBlockFieldMemberInfo(const TFieldList &fields,
sh::BlockLayoutEncoder *encoder,
TLayoutBlockStorage storage);
TLayoutBlockStorage storage,
BlockMemberInfoMap *blockInfoOut);

size_t SetBlockFieldMemberInfoAndReturnBlockSize(const TFieldList &fields,
TLayoutBlockStorage storage)
size_t GetBlockFieldMemberInfoAndReturnBlockSize(const TFieldList &fields,
TLayoutBlockStorage storage,
BlockMemberInfoMap *blockInfoOut)
{
sh::Std140BlockEncoder std140Encoder;
sh::HLSLBlockEncoder hlslEncoder(sh::HLSLBlockEncoder::ENCODE_PACKED, false);
Expand All @@ -70,31 +71,31 @@ size_t SetBlockFieldMemberInfoAndReturnBlockSize(const TFieldList &fields,
structureEncoder = &hlslEncoder;
}

SetShaderStorageBlockFieldMemberInfo(fields, structureEncoder, storage);
GetShaderStorageBlockFieldMemberInfo(fields, structureEncoder, storage, blockInfoOut);
structureEncoder->exitAggregateType();
return structureEncoder->getBlockSize();
}

// TODO(jiajia.qin@intel.com): Save the offset/arrystride into a std::map<const TField *,
// BlockMemberInfo> BlockLayoutMap instead of adding offset/arrayStride to the TField.
// TODO(jiajia.qin@intel.com): use correct row major attribute for structure field member.
// http://anglebug.com/1951
void SetShaderStorageBlockFieldMemberInfo(const TFieldList &fields,
void GetShaderStorageBlockFieldMemberInfo(const TFieldList &fields,
sh::BlockLayoutEncoder *encoder,
TLayoutBlockStorage storage)
TLayoutBlockStorage storage,
BlockMemberInfoMap *blockInfoOut)
{
for (TField *field : fields)
for (const TField *field : fields)
{
const TType &fieldType = *field->type();
if (fieldType.getStruct())
{
encoder->enterAggregateType();
field->setOffset(encoder->getBlockSize());

// This is to set structure member offset and array stride using a new encoder to ensure
// that the first field member offset in structure is always zero.
size_t structureStride =
SetBlockFieldMemberInfoAndReturnBlockSize(fieldType.getStruct()->fields(), storage);
field->setArrayStride(structureStride);
size_t structureStride = GetBlockFieldMemberInfoAndReturnBlockSize(
fieldType.getStruct()->fields(), storage, blockInfoOut);
const BlockMemberInfo memberInfo(static_cast<int>(encoder->getBlockSize()),
static_cast<int>(structureStride), 0, false);
(*blockInfoOut)[field] = memberInfo;

// Below if-else is in order to get correct offset for the field members after structure
// field.
Expand Down Expand Up @@ -124,13 +125,13 @@ void SetShaderStorageBlockFieldMemberInfo(const TFieldList &fields,
const BlockMemberInfo &memberInfo =
encoder->encodeType(GLVariableType(fieldType), fieldArraySizes,
isRowMajorLayout && fieldType.isMatrix());
field->setOffset(memberInfo.offset);
field->setArrayStride(memberInfo.arrayStride);
(*blockInfoOut)[field] = memberInfo;
}
}
}

void SetShaderStorageBlockMembersOffset(const TInterfaceBlock *interfaceBlock)
void GetShaderStorageBlockMembersInfo(const TInterfaceBlock *interfaceBlock,
BlockMemberInfoMap *blockInfoOut)
{
sh::Std140BlockEncoder std140Encoder;
sh::HLSLBlockEncoder hlslEncoder(sh::HLSLBlockEncoder::ENCODE_PACKED, false);
Expand All @@ -146,8 +147,8 @@ void SetShaderStorageBlockMembersOffset(const TInterfaceBlock *interfaceBlock)
encoder = &hlslEncoder;
}

SetShaderStorageBlockFieldMemberInfo(interfaceBlock->fields(), encoder,
interfaceBlock->blockStorage());
GetShaderStorageBlockFieldMemberInfo(interfaceBlock->fields(), encoder,
interfaceBlock->blockStorage(), blockInfoOut);
}

bool IsInArrayOfArraysChain(TIntermTyped *node)
Expand Down Expand Up @@ -262,7 +263,7 @@ void ShaderStorageBlockOutputHLSL::visitSymbol(TIntermSymbol *node)
}
mReferencedShaderStorageBlocks[interfaceBlock->uniqueId().get()] =
new TReferencedBlock(interfaceBlock, instanceVariable);
SetShaderStorageBlockMembersOffset(interfaceBlock);
GetShaderStorageBlockMembersInfo(interfaceBlock, &mBlockMemberInfoMap);
}
if (variableType.isInterfaceBlock())
{
Expand Down Expand Up @@ -349,7 +350,7 @@ bool ShaderStorageBlockOutputHLSL::visitBinary(Visit visit, TIntermBinary *node)
{
mReferencedShaderStorageBlocks[interfaceBlock->uniqueId().get()] =
new TReferencedBlock(interfaceBlock, &instanceArraySymbol->variable());
SetShaderStorageBlockMembersOffset(interfaceBlock);
GetShaderStorageBlockMembersInfo(interfaceBlock, &mBlockMemberInfoMap);
}

const int arrayIndex = node->getRight()->getAsConstantUnion()->getIConst(0);
Expand Down Expand Up @@ -482,13 +483,16 @@ void ShaderStorageBlockOutputHLSL::writeEOpIndexDirectOrIndirectOutput(TInfoSink

void ShaderStorageBlockOutputHLSL::writeDotOperatorOutput(TInfoSinkBase &out, const TField *field)
{
out << str(field->getOffset());
auto fieldInfoIter = mBlockMemberInfoMap.find(field);
ASSERT(fieldInfoIter != mBlockMemberInfoMap.end());
const BlockMemberInfo &memberInfo = fieldInfoIter->second;
out << memberInfo.offset;

const TType &fieldType = *field->type();
if (fieldType.isArray() && !isEndOfSSBOAccessChain())
{
out << " + ";
out << field->getArrayStride();
out << memberInfo.arrayStride;
if (fieldType.isArrayOfArrays())
{
out << " * (";
Expand Down
6 changes: 6 additions & 0 deletions src/compiler/translator/ShaderStorageBlockOutputHLSL.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#define COMPILER_TRANSLATOR_SHADERSTORAGEBLOCKOUTPUTHLSL_H_

#include "compiler/translator/ShaderStorageBlockFunctionHLSL.h"
#include "compiler/translator/blocklayout.h"
#include "compiler/translator/tree_util/IntermTraverse.h"

namespace sh
Expand All @@ -30,6 +31,9 @@ struct TReferencedBlock : angle::NonCopyable
// Maps from uniqueId to a variable.
using ReferencedInterfaceBlocks = std::map<int, const TReferencedBlock *>;

// Used to save shader storage block field member information.
using BlockMemberInfoMap = std::map<const TField *, BlockMemberInfo>;

class ShaderStorageBlockOutputHLSL : public TIntermTraverser
{
public:
Expand Down Expand Up @@ -69,6 +73,8 @@ class ShaderStorageBlockOutputHLSL : public TIntermTraverser
ShaderStorageBlockFunctionHLSL *mSSBOFunctionHLSL;
ResourcesHLSL *mResourcesHLSL;
ReferencedInterfaceBlocks mReferencedShaderStorageBlocks;

BlockMemberInfoMap mBlockMemberInfoMap;
};
}

Expand Down
13 changes: 1 addition & 12 deletions src/compiler/translator/Types.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,7 @@ class TField : angle::NonCopyable
public:
POOL_ALLOCATOR_NEW_DELETE();
TField(TType *type, const ImmutableString &name, const TSourceLoc &line, SymbolType symbolType)
: mType(type),
mName(name),
mLine(line),
mSymbolType(symbolType),
mOffset(0),
mArrayStride(0)
: mType(type), mName(name), mLine(line), mSymbolType(symbolType)
{
ASSERT(mSymbolType != SymbolType::Empty);
}
Expand All @@ -49,18 +44,12 @@ class TField : angle::NonCopyable
const ImmutableString &name() const { return mName; }
const TSourceLoc &line() const { return mLine; }
SymbolType symbolType() const { return mSymbolType; }
unsigned int getOffset() const { return mOffset; }
unsigned int getArrayStride() const { return mArrayStride; }
void setOffset(unsigned int offset) { mOffset = offset; }
void setArrayStride(int arrayStride) { mArrayStride = arrayStride; }

private:
TType *mType;
const ImmutableString mName;
const TSourceLoc mLine;
const SymbolType mSymbolType;
unsigned int mOffset;
unsigned int mArrayStride;
};

typedef TVector<TField *> TFieldList;
Expand Down

0 comments on commit df73a8e

Please sign in to comment.