Commit 8047c0d2 by Jamie Madill Committed by Commit Bot

D3D11: Clean up InputLayoutCache.

This change does a couple things. First, it uses the 'active attribs' mask in the gl::Program to sort the translated attributes, instead of checking the translated attribute themselves. This means we don't have to consult the 'active' field of the translated attributes, which in turns means we don't have to update the active field in the attributes, which breaks the dependency of the attributes on the gl::Program. Second, use a dynamically sized array for storing the cached vertex attributes in the InputLayoutCache. This is nice because it means we don't have to store the size of the array separately. Also some other refactoring cleanups. Refactoring change only. BUG=angleproject:1327 Change-Id: Iab22de92840b30674b92eca72e450673ed9f6d6d Reviewed-on: https://chromium-review.googlesource.com/330172Reviewed-by: 's avatarZhenyao Mo <zmo@chromium.org> Commit-Queue: Jamie Madill <jmadill@chromium.org>
parent e9e15349
......@@ -79,28 +79,6 @@ bool IsRowMajorLayout(const sh::ShaderVariable &var)
return false;
}
struct AttributeSorter
{
AttributeSorter(const ProgramD3D::SemanticIndexArray &semanticIndices)
: originalIndices(&semanticIndices)
{
}
bool operator()(int a, int b)
{
int indexA = (*originalIndices)[a];
int indexB = (*originalIndices)[b];
if (indexA == -1)
return false;
if (indexB == -1)
return true;
return (indexA < indexB);
}
const ProgramD3D::SemanticIndexArray *originalIndices;
};
// true if varying x has a higher priority in packing than y
bool ComparePackedVarying(const PackedVarying &x, const PackedVarying &y)
{
......@@ -772,10 +750,9 @@ LinkResult ProgramD3D::load(gl::InfoLog &infoLog, gl::BinaryInputStream *stream)
return LinkResult(false, gl::Error(GL_NO_ERROR));
}
// TODO(jmadill): replace MAX_VERTEX_ATTRIBS
for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; ++i)
for (int &index : mAttribLocationToD3DSemantic)
{
stream->readInt(&mSemanticIndexes[i]);
stream->readInt(&index);
}
const unsigned int psSamplerCount = stream->readInt<unsigned int>();
......@@ -983,7 +960,6 @@ LinkResult ProgramD3D::load(gl::InfoLog &infoLog, gl::BinaryInputStream *stream)
}
initializeUniformStorage();
initAttributesByLayout();
return LinkResult(true, gl::Error(GL_NO_ERROR));
}
......@@ -999,10 +975,9 @@ gl::Error ProgramD3D::save(gl::BinaryOutputStream *stream)
stream->writeInt(ANGLE_COMPILE_OPTIMIZATION_LEVEL);
// TODO(jmadill): replace MAX_VERTEX_ATTRIBS
for (unsigned int i = 0; i < gl::MAX_VERTEX_ATTRIBS; ++i)
for (int d3dSemantic : mAttribLocationToD3DSemantic)
{
stream->writeInt(mSemanticIndexes[i]);
stream->writeInt(d3dSemantic);
}
stream->writeInt(mSamplersPS.size());
......@@ -1465,7 +1440,7 @@ LinkResult ProgramD3D::link(const gl::Data &data, gl::InfoLog &infoLog)
mGeometryShaderPreamble = mDynamicHLSL->generateGeometryShaderPreamble(varyingPacking);
}
initSemanticIndex();
initAttribLocationsToD3DSemantic();
defineUniformsAndAssignRegisters();
......@@ -2173,8 +2148,7 @@ void ProgramD3D::reset()
mUsedPixelSamplerRange = 0;
mDirtySamplerMapping = true;
std::fill(mSemanticIndexes, mSemanticIndexes + ArraySize(mSemanticIndexes), -1);
std::fill(mAttributesByLayout, mAttributesByLayout + ArraySize(mAttributesByLayout), -1);
mAttribLocationToD3DSemantic.fill(-1);
mStreamOutVaryings.clear();
......@@ -2191,7 +2165,7 @@ unsigned int ProgramD3D::issueSerial()
return mCurrentSerial++;
}
void ProgramD3D::initSemanticIndex()
void ProgramD3D::initAttribLocationsToD3DSemantic()
{
const gl::Shader *vertexShader = mData.getAttachedVertexShader();
ASSERT(vertexShader != nullptr);
......@@ -2199,41 +2173,14 @@ void ProgramD3D::initSemanticIndex()
// Init semantic index
for (const sh::Attribute &attribute : mData.getAttributes())
{
int attributeIndex = attribute.location;
int index = vertexShader->getSemanticIndex(attribute.name);
int regs = gl::VariableRegisterCount(attribute.type);
int d3dSemantic = vertexShader->getSemanticIndex(attribute.name);
int regCount = gl::VariableRegisterCount(attribute.type);
for (int reg = 0; reg < regs; ++reg)
for (int reg = 0; reg < regCount; ++reg)
{
mSemanticIndexes[attributeIndex + reg] = index + reg;
mAttribLocationToD3DSemantic[attribute.location + reg] = d3dSemantic + reg;
}
}
initAttributesByLayout();
}
void ProgramD3D::initAttributesByLayout()
{
for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
{
mAttributesByLayout[i] = i;
}
std::sort(&mAttributesByLayout[0], &mAttributesByLayout[gl::MAX_VERTEX_ATTRIBS],
AttributeSorter(mSemanticIndexes));
}
void ProgramD3D::sortAttributesByLayout(
const std::vector<TranslatedAttribute> &unsortedAttributes,
int sortedSemanticIndicesOut[gl::MAX_VERTEX_ATTRIBS],
const rx::TranslatedAttribute *sortedAttributesOut[gl::MAX_VERTEX_ATTRIBS]) const
{
for (size_t attribIndex = 0; attribIndex < unsortedAttributes.size(); ++attribIndex)
{
int oldIndex = mAttributesByLayout[attribIndex];
sortedSemanticIndicesOut[attribIndex] = mSemanticIndexes[oldIndex];
sortedAttributesOut[attribIndex] = &unsortedAttributes[oldIndex];
}
}
void ProgramD3D::updateCachedInputLayout(const gl::State &state)
......@@ -2241,19 +2188,19 @@ void ProgramD3D::updateCachedInputLayout(const gl::State &state)
mCachedInputLayout.clear();
const auto &vertexAttributes = state.getVertexArray()->getVertexAttributes();
for (unsigned int attributeIndex : angle::IterateBitSet(mData.getActiveAttribLocationsMask()))
for (unsigned int locationIndex : angle::IterateBitSet(mData.getActiveAttribLocationsMask()))
{
int semanticIndex = mSemanticIndexes[attributeIndex];
int d3dSemantic = mAttribLocationToD3DSemantic[locationIndex];
if (semanticIndex != -1)
if (d3dSemantic != -1)
{
if (mCachedInputLayout.size() < static_cast<size_t>(semanticIndex + 1))
if (mCachedInputLayout.size() < static_cast<size_t>(d3dSemantic + 1))
{
mCachedInputLayout.resize(semanticIndex + 1, gl::VERTEX_FORMAT_INVALID);
mCachedInputLayout.resize(d3dSemantic + 1, gl::VERTEX_FORMAT_INVALID);
}
mCachedInputLayout[semanticIndex] =
GetVertexFormatType(vertexAttributes[attributeIndex],
state.getVertexAttribCurrentValue(attributeIndex).Type);
mCachedInputLayout[d3dSemantic] =
GetVertexFormatType(vertexAttributes[locationIndex],
state.getVertexAttribCurrentValue(locationIndex).Type);
}
}
}
......@@ -2357,4 +2304,4 @@ bool ProgramD3D::getUniformBlockMemberInfo(const std::string &memberUniformName,
*memberInfoOut = infoIter->second;
return true;
}
}
} // namespace rx
......@@ -134,8 +134,6 @@ class ProgramD3DMetadata : angle::NonCopyable
class ProgramD3D : public ProgramImpl
{
public:
typedef int SemanticIndexArray[gl::MAX_VERTEX_ATTRIBS];
ProgramD3D(const gl::Program::Data &data, RendererD3D *renderer);
virtual ~ProgramD3D();
......@@ -238,12 +236,10 @@ class ProgramD3D : public ProgramImpl
unsigned int getSerial() const;
void sortAttributesByLayout(
const std::vector<TranslatedAttribute> &unsortedAttributes,
int sortedSemanticIndicesOut[gl::MAX_VERTEX_ATTRIBS],
const rx::TranslatedAttribute *sortedAttributesOut[gl::MAX_VERTEX_ATTRIBS]) const;
const SemanticIndexArray &getSemanticIndexes() const { return mSemanticIndexes; }
const SemanticIndexArray &getAttributesByLayout() const { return mAttributesByLayout; }
const AttribIndexArray &getAttribLocationToD3DSemantics() const
{
return mAttribLocationToD3DSemantic;
}
void updateCachedInputLayout(const gl::State &state);
const gl::InputLayout &getCachedInputLayout() const { return mCachedInputLayout; }
......@@ -350,8 +346,7 @@ class ProgramD3D : public ProgramImpl
D3DUniform *getD3DUniformByName(const std::string &name);
D3DUniform *getD3DUniformFromLocation(GLint location);
void initSemanticIndex();
void initAttributesByLayout();
void initAttribLocationsToD3DSemantic();
void reset();
void assignUniformBlockRegisters();
......@@ -394,8 +389,7 @@ class ProgramD3D : public ProgramImpl
// Cache for getPixelExecutableForFramebuffer
std::vector<GLenum> mPixelShaderOutputFormatCache;
SemanticIndexArray mSemanticIndexes;
SemanticIndexArray mAttributesByLayout;
AttribIndexArray mAttribLocationToD3DSemantic;
unsigned int mSerial;
......
......@@ -94,6 +94,8 @@ class BufferFactoryD3D
GLsizei instances) const = 0;
};
using AttribIndexArray = std::array<int, gl::MAX_VERTEX_ATTRIBS>;
class RendererD3D : public Renderer, public BufferFactoryD3D
{
public:
......
......@@ -21,6 +21,7 @@
#include "libANGLE/Constants.h"
#include "libANGLE/Error.h"
#include "libANGLE/formatutils.h"
#include "libANGLE/renderer/d3d/RendererD3D.h"
namespace gl
{
......@@ -34,9 +35,6 @@ struct TranslatedIndexData;
struct SourceIndexData;
class ProgramD3D;
using SortedAttribArray = std::array<const TranslatedAttribute *, gl::MAX_VERTEX_ATTRIBS>;
using SortedIndexArray = std::array<int, gl::MAX_VERTEX_ATTRIBS>;
class InputLayoutCache : angle::NonCopyable
{
public:
......@@ -47,9 +45,9 @@ class InputLayoutCache : angle::NonCopyable
void clear();
void markDirty();
gl::Error applyVertexBuffers(const std::vector<TranslatedAttribute> &attributes,
gl::Error applyVertexBuffers(const gl::State &state,
const std::vector<TranslatedAttribute> &attributes,
GLenum mode,
gl::Program *program,
TranslatedIndexData *indexInfo,
GLsizei numIndicesPerInstance);
......@@ -86,15 +84,11 @@ class InputLayoutCache : angle::NonCopyable
uint32_t attributeData[gl::MAX_VERTEX_ATTRIBS];
};
gl::Error updateInputLayout(gl::Program *program,
gl::Error updateInputLayout(const gl::State &state,
GLenum mode,
const SortedAttribArray &sortedAttributes,
const SortedIndexArray &sortedSemanticIndices,
size_t attribCount,
const AttribIndexArray &sortedSemanticIndices,
GLsizei numIndicesPerInstance);
gl::Error createInputLayout(const SortedAttribArray &sortedAttributes,
const SortedIndexArray &sortedSemanticIndices,
size_t attribCount,
gl::Error createInputLayout(const AttribIndexArray &sortedSemanticIndices,
GLenum mode,
gl::Program *program,
GLsizei numIndicesPerInstance,
......@@ -103,11 +97,10 @@ class InputLayoutCache : angle::NonCopyable
std::map<PackedAttributeLayout, ID3D11InputLayout *> mLayoutMap;
ID3D11InputLayout *mCurrentIL;
ID3D11Buffer *mCurrentBuffers[gl::MAX_VERTEX_ATTRIBS];
UINT mCurrentVertexStrides[gl::MAX_VERTEX_ATTRIBS];
UINT mCurrentVertexOffsets[gl::MAX_VERTEX_ATTRIBS];
SortedAttribArray mSortedAttributes;
size_t mUnsortedAttributesCount;
std::array<ID3D11Buffer *, gl::MAX_VERTEX_ATTRIBS> mCurrentBuffers;
std::array<UINT, gl::MAX_VERTEX_ATTRIBS> mCurrentVertexStrides;
std::array<UINT, gl::MAX_VERTEX_ATTRIBS> mCurrentVertexOffsets;
std::vector<const TranslatedAttribute *> mCurrentAttributes;
ID3D11Buffer *mPointSpriteVertexBuffer;
ID3D11Buffer *mPointSpriteIndexBuffer;
......
......@@ -1515,8 +1515,8 @@ gl::Error Renderer11::applyVertexBuffer(const gl::State &state,
{
numIndicesPerInstance = count;
}
return mInputLayoutCache.applyVertexBuffers(mTranslatedAttribCache, mode, state.getProgram(),
indexInfo, numIndicesPerInstance);
return mInputLayoutCache.applyVertexBuffers(state, mTranslatedAttribCache, mode, indexInfo,
numIndicesPerInstance);
}
gl::Error Renderer11::applyIndexBuffer(const gl::Data &data,
......
......@@ -102,7 +102,7 @@ gl::Error VertexDeclarationCache::applyDeclaration(IDirect3DDevice9 *device,
D3DVERTEXELEMENT9 *element = &elements[0];
ProgramD3D *programD3D = GetImplAs<ProgramD3D>(program);
const auto &semanticIndexes = programD3D->getSemanticIndexes();
const auto &semanticIndexes = programD3D->getAttribLocationToD3DSemantics();
for (size_t i = 0; i < attributes.size(); i++)
{
......
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