Caching of shader program attribute and uniform IDs from strings.

This commit is contained in:
Vojtech Bubnik 2021-12-06 11:45:02 +01:00
parent a952fbc31a
commit 68d5a47121
2 changed files with 30 additions and 2 deletions

View File

@ -358,12 +358,38 @@ bool GLShaderProgram::set_uniform(const char* name, const Vec3d& value) const
int GLShaderProgram::get_attrib_location(const char* name) const
{
return (m_id > 0) ? ::glGetAttribLocation(m_id, name) : -1;
assert(m_id > 0);
if (m_id <= 0)
// Shader program not loaded. This should not happen.
return -1;
auto it = std::find_if(m_attrib_location_cache.begin(), m_attrib_location_cache.end(), [name](const auto& p) { return p.first == name; });
if (it != m_attrib_location_cache.end())
// Attrib ID cached.
return it->second;
int id = ::glGetAttribLocation(m_id, name);
const_cast<GLShaderProgram*>(this)->m_attrib_location_cache.push_back({ name, id });
return id;
}
int GLShaderProgram::get_uniform_location(const char* name) const
{
return (m_id > 0) ? ::glGetUniformLocation(m_id, name) : -1;
assert(m_id > 0);
if (m_id <= 0)
// Shader program not loaded. This should not happen.
return -1;
auto it = std::find_if(m_uniform_location_cache.begin(), m_uniform_location_cache.end(), [name](const auto &p) { return p.first == name; });
if (it != m_uniform_location_cache.end())
// Uniform ID cached.
return it->second;
int id = ::glGetUniformLocation(m_id, name);
const_cast<GLShaderProgram*>(this)->m_uniform_location_cache.push_back({ name, id });
return id;
}
} // namespace Slic3r

View File

@ -29,6 +29,8 @@ public:
private:
std::string m_name;
unsigned int m_id{ 0 };
std::vector<std::pair<std::string, int>> m_attrib_location_cache;
std::vector<std::pair<std::string, int>> m_uniform_location_cache;
public:
~GLShaderProgram();