Change Program::getActiveAttributes retun type to reference

This commit is contained in:
halx99 2022-09-24 11:38:41 +08:00
parent 0e2194a247
commit c312a9c157
9 changed files with 22 additions and 27 deletions

View File

@ -352,7 +352,7 @@ void Mesh::setMaterial(Material* material)
if (_material->getTechnique()->getName().compare(technique->getName()) == 0)
{
auto program = pass->getProgramState()->getProgram();
auto attributes = program->getActiveAttributes();
auto& attributes = program->getActiveAttributes();
auto meshVertexData = _meshIndexData->getMeshVertexData();
auto attributeCount = meshVertexData->getMeshVertexAttribCount();
AXASSERT(attributes.size() <= attributeCount, "missing attribute data");

View File

@ -31,7 +31,7 @@ NS_AX_BEGIN
static std::vector<VertexAttribBinding*> __vertexAttribBindingCache;
VertexAttribBinding::VertexAttribBinding() : _meshIndexData(nullptr), _programState(nullptr), _attributes() {}
VertexAttribBinding::VertexAttribBinding() : _meshIndexData(nullptr), _programState(nullptr) {}
VertexAttribBinding::~VertexAttribBinding()
{
@ -45,7 +45,6 @@ VertexAttribBinding::~VertexAttribBinding()
AX_SAFE_RELEASE(_meshIndexData);
AX_SAFE_RELEASE(_programState);
_attributes.clear();
}
VertexAttribBinding* VertexAttribBinding::create(MeshIndexData* meshIndexData, Pass* pass, MeshCommand* command)
@ -115,23 +114,21 @@ void VertexAttribBinding::parseAttributes()
{
AXASSERT(_programState, "invalid glprogram");
_attributes.clear();
_vertexAttribsFlags = 0;
auto program = _programState->getProgram();
_attributes = program->getActiveAttributes();
}
bool VertexAttribBinding::hasAttribute(const shaderinfos::VertexKey& key) const
{
auto& name = shaderinfos::getAttributeName(key);
return _attributes.find(name) != _attributes.end();
auto& attributes = _programState->getProgram()->getActiveAttributes();
return attributes.find(name) != attributes.end();
}
backend::AttributeBindInfo* VertexAttribBinding::getVertexAttribValue(std::string_view name)
const backend::AttributeBindInfo* VertexAttribBinding::getVertexAttribValue(std::string_view name)
{
const auto itr = _attributes.find(name);
if (itr != _attributes.end())
auto& attributes = _programState->getProgram()->getActiveAttributes();
const auto itr = attributes.find(name);
if (itr != attributes.end())
return &itr->second;
return nullptr;
}

View File

@ -112,12 +112,11 @@ private:
bool normalized,
int offset,
int flag);
backend::AttributeBindInfo* getVertexAttribValue(std::string_view name);
const backend::AttributeBindInfo* getVertexAttribValue(std::string_view name);
void parseAttributes();
MeshIndexData* _meshIndexData;
backend::ProgramState* _programState;
hlookup::string_map<backend::AttributeBindInfo> _attributes;
uint32_t _vertexAttribsFlags;
};

View File

@ -101,7 +101,7 @@ public:
* Get active vertex attributes.
* @return Active vertex attributes. key is active attribute name, Value is corresponding attribute info.
*/
virtual const hlookup::string_map<AttributeBindInfo> getActiveAttributes() const = 0;
virtual const hlookup::string_map<AttributeBindInfo>& getActiveAttributes() const = 0;
/**
* Get vertex shader.

View File

@ -94,7 +94,7 @@ public:
* Get active vertex attributes.
* @return Active vertex attributes. key is active attribute name, Value is corresponding attribute info.
*/
const hlookup::string_map<AttributeBindInfo> getActiveAttributes() const override;
const hlookup::string_map<AttributeBindInfo>& getActiveAttributes() const override;
/**
* Get maximum vertex location.

View File

@ -118,7 +118,7 @@ int ProgramMTL::getMaxFragmentLocation() const
return _fragmentShader->getMaxLocation();
}
const hlookup::string_map<AttributeBindInfo> ProgramMTL::getActiveAttributes() const
const hlookup::string_map<AttributeBindInfo>& ProgramMTL::getActiveAttributes() const
{
return _vertexShader->getAttributeInfo();
}

View File

@ -83,7 +83,7 @@ public:
* Get active attribute informations.
* @return Active attribute informations. key is attribute name and Value is corresponding attribute info.
*/
inline const hlookup::string_map<AttributeBindInfo> getAttributeInfo() const { return _attributeInfo; }
inline const hlookup::string_map<AttributeBindInfo>& getAttributeInfo() const { return _attributeInfo; }
/**
* Get uniform location by engine built-in uniform enum name.

View File

@ -221,20 +221,18 @@ bool ProgramGL::getAttributeLocation(std::string_view attributeName, unsigned in
return true;
}
const hlookup::string_map<AttributeBindInfo> ProgramGL::getActiveAttributes() const
const hlookup::string_map<AttributeBindInfo>& ProgramGL::getActiveAttributes() const
{
hlookup::string_map<AttributeBindInfo> attributes;
if (!_program)
return attributes;
if (!_program || !_activeAttribs.empty())
return _activeAttribs;
GLint numOfActiveAttributes = 0;
glGetProgramiv(_program, GL_ACTIVE_ATTRIBUTES, &numOfActiveAttributes);
if (numOfActiveAttributes <= 0)
return attributes;
return _activeAttribs;
attributes.reserve(numOfActiveAttributes);
_activeAttribs.reserve(numOfActiveAttributes);
int MAX_ATTRIBUTE_NAME_LENGTH = 256;
std::vector<char> attrName(MAX_ATTRIBUTE_NAME_LENGTH + 1);
@ -253,10 +251,10 @@ const hlookup::string_map<AttributeBindInfo> ProgramGL::getActiveAttributes() co
info.type = attrType;
info.size = UtilsGL::getGLDataTypeSize(attrType) * attrSize;
CHECK_GL_ERROR_DEBUG();
attributes[info.attributeName] = info;
_activeAttribs[info.attributeName] = info;
}
return attributes;
return _activeAttribs;
}
void ProgramGL::computeUniformInfos()

View File

@ -128,7 +128,7 @@ public:
* Get active vertex attributes.
* @return Active vertex attributes. key is active attribute name, Value is corresponding attribute info.
*/
virtual const hlookup::string_map<AttributeBindInfo> getActiveAttributes() const override;
virtual const hlookup::string_map<AttributeBindInfo>& getActiveAttributes() const override;
/**
* Get uniform buffer size in bytes that can hold all the uniforms.
@ -173,6 +173,7 @@ private:
std::vector<AttributeInfo> _attributeInfos;
hlookup::string_map<UniformInfo> _activeUniformInfos;
mutable hlookup::string_map<AttributeBindInfo> _activeAttribs;
#if AX_ENABLE_CACHE_TEXTURE_DATA
std::unordered_map<std::string, int>
_originalUniformLocations; ///< record the uniform location when shader was first created.