Optimize program uniform location cache

This commit is contained in:
Nikita Lisitsa 2021-07-13 11:53:58 +03:00
parent 1cc5d28761
commit d73ce27c32
2 changed files with 10 additions and 8 deletions

View file

@ -8,7 +8,8 @@
#include <psemek/geom/interval.hpp> #include <psemek/geom/interval.hpp>
#include <psemek/geom/quaternion.hpp> #include <psemek/geom/quaternion.hpp>
#include <unordered_map> #include <boost/container/flat_map.hpp>
#include <string_view> #include <string_view>
namespace psemek::gfx namespace psemek::gfx
@ -36,7 +37,7 @@ namespace psemek::gfx
void reset(); void reset();
GLint location(char const * name) const; GLint location(std::string_view name) const;
struct uniform_proxy struct uniform_proxy
{ {
@ -110,11 +111,11 @@ namespace psemek::gfx
GLint location_; GLint location_;
}; };
uniform_proxy operator[] (char const * name) const; uniform_proxy operator[] (std::string_view name) const;
private: private:
GLuint program_; GLuint program_;
mutable std::unordered_map<std::string, GLint> uniforms_; mutable boost::container::flat_map<std::string, GLint, std::less<>> uniforms_;
program(std::nullptr_t) program(std::nullptr_t)
: program_{0} : program_{0}

View file

@ -364,19 +364,20 @@ namespace psemek::gfx
program_ = 0; program_ = 0;
} }
GLint program::location(char const * name) const GLint program::location(std::string_view name) const
{ {
auto it = uniforms_.find(name); auto it = uniforms_.find(name);
if (it == uniforms_.end()) if (it == uniforms_.end())
{ {
auto l = gl::GetUniformLocation(program_, name); std::string name_str(name.begin(), name.end());
uniforms_[name] = l; auto l = gl::GetUniformLocation(program_, name_str.c_str());
uniforms_[std::move(name_str)] = l;
return l; return l;
} }
return it->second; return it->second;
} }
program::uniform_proxy program::operator[] (char const * name) const program::uniform_proxy program::operator[] (std::string_view name) const
{ {
return {location(name)}; return {location(name)};
} }