diff --git a/include/mbgl/gfx/shader_group.hpp b/include/mbgl/gfx/shader_group.hpp index 4ecb65f33..a271c1b9b 100644 --- a/include/mbgl/gfx/shader_group.hpp +++ b/include/mbgl/gfx/shader_group.hpp @@ -2,6 +2,7 @@ #include #include +#include #include #include @@ -154,10 +155,19 @@ public: } protected: - std::string getShaderName(const std::string_view& name, const std::size_t key) { + using PropertyHashType = std::uint64_t; + + std::string getShaderName(const std::string_view& name, const PropertyHashType key) { return (std::ostringstream() << name << '#' << std::hex << key).str(); } + /// Generate a map key for the specified combination of properties + PropertyHashType propertyHash(const mbgl::unordered_set& propertiesAsUniforms) { + const auto beg = propertiesAsUniforms.cbegin(); + const auto end = propertiesAsUniforms.cend(); + return util::order_independent_hash(beg, end); + } + private: mbgl::unordered_map> programs; mutable std::shared_mutex programLock; diff --git a/include/mbgl/shaders/gl/shader_group_gl.hpp b/include/mbgl/shaders/gl/shader_group_gl.hpp index e7489fb7f..a1c2367df 100644 --- a/include/mbgl/shaders/gl/shader_group_gl.hpp +++ b/include/mbgl/shaders/gl/shader_group_gl.hpp @@ -4,7 +4,6 @@ #include #include #include -#include #include namespace mbgl { @@ -25,12 +24,9 @@ public: constexpr auto& vert = shaders::ShaderSource::vertex; constexpr auto& frag = shaders::ShaderSource::fragment; - // Generate a map key for the specified combination of properties - const size_t key = util::order_independent_hash(propertiesAsUniforms.begin(), propertiesAsUniforms.end()); - // We could cache these by key here to avoid creating a string key each time, but we // would need another mutex. We could also push string IDs down into `ShaderGroup`. - const std::string shaderName = getShaderName(name, key); + const std::string shaderName = getShaderName(name, propertyHash(propertiesAsUniforms)); auto shader = get(shaderName); if (shader) { return shader; diff --git a/include/mbgl/shaders/mtl/shader_group.hpp b/include/mbgl/shaders/mtl/shader_group.hpp index 6726bc71d..e55f00af6 100644 --- a/include/mbgl/shaders/mtl/shader_group.hpp +++ b/include/mbgl/shaders/mtl/shader_group.hpp @@ -52,9 +52,7 @@ public: constexpr auto& fragMain = ShaderSource::fragmentMainFunction; constexpr auto permutations = shaders::ShaderSource::hasPermutations; - const size_t key = permutations - ? util::order_independent_hash(propertiesAsUniforms.begin(), propertiesAsUniforms.end()) - : 0; + const PropertyHashType key = permutations ? propertyHash(propertiesAsUniforms) : 0; const std::string shaderName = permutations ? getShaderName(name, key) : name; auto shader = get(shaderName); diff --git a/src/mbgl/util/hash.hpp b/src/mbgl/util/hash.hpp index 2786d8d46..f35b15d47 100644 --- a/src/mbgl/util/hash.hpp +++ b/src/mbgl/util/hash.hpp @@ -36,7 +36,7 @@ constexpr T factor() { /// Generate a hash key from a collection of integer values which doesn't depend on their order. /// Adapted from https://stackoverflow.com/a/76993810/135138 template , TKey factor = factor()> -TKey order_independent_hash(TIter cur, const TIter end) { +TKey order_independent_hash(std::remove_const_t cur, const TIter end) { detail::TIterVal sum = 0, product = 1; for (; cur != end; ++cur) { const auto& value = *cur;