From 68d5a47121eaad42a390fe8d7b85cbff551cf90e Mon Sep 17 00:00:00 2001 From: Vojtech Bubnik Date: Mon, 6 Dec 2021 11:45:02 +0100 Subject: [PATCH] Caching of shader program attribute and uniform IDs from strings. --- src/slic3r/GUI/GLShader.cpp | 30 ++++++++++++++++++++++++++++-- src/slic3r/GUI/GLShader.hpp | 2 ++ 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/src/slic3r/GUI/GLShader.cpp b/src/slic3r/GUI/GLShader.cpp index 0473e5344..9c1e93652 100644 --- a/src/slic3r/GUI/GLShader.cpp +++ b/src/slic3r/GUI/GLShader.cpp @@ -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(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(this)->m_uniform_location_cache.push_back({ name, id }); + return id; } } // namespace Slic3r diff --git a/src/slic3r/GUI/GLShader.hpp b/src/slic3r/GUI/GLShader.hpp index c92ea274a..d7b92000d 100644 --- a/src/slic3r/GUI/GLShader.hpp +++ b/src/slic3r/GUI/GLShader.hpp @@ -29,6 +29,8 @@ public: private: std::string m_name; unsigned int m_id{ 0 }; + std::vector> m_attrib_location_cache; + std::vector> m_uniform_location_cache; public: ~GLShaderProgram();