Skip to content

Commit

Permalink
Use 64-bit hashes regardless of platform (#1918)
Browse files Browse the repository at this point in the history
  • Loading branch information
TimSylvester authored Dec 5, 2023
1 parent a92bc67 commit f7666f4
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 10 deletions.
12 changes: 11 additions & 1 deletion include/mbgl/gfx/shader_group.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <mbgl/gfx/shader.hpp>
#include <mbgl/util/containers.hpp>
#include <mbgl/util/hash.hpp>

#include <iomanip>
#include <memory>
Expand Down Expand Up @@ -154,10 +155,19 @@ class ShaderGroup {
}

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<StringIdentity>& propertiesAsUniforms) {
const auto beg = propertiesAsUniforms.cbegin();
const auto end = propertiesAsUniforms.cend();
return util::order_independent_hash<decltype(beg), PropertyHashType>(beg, end);
}

private:
mbgl::unordered_map<std::string, std::shared_ptr<gfx::Shader>> programs;
mutable std::shared_mutex programLock;
Expand Down
6 changes: 1 addition & 5 deletions include/mbgl/shaders/gl/shader_group_gl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#include <mbgl/shaders/gl/shader_program_gl.hpp>
#include <mbgl/shaders/shader_source.hpp>
#include <mbgl/programs/program_parameters.hpp>
#include <mbgl/util/hash.hpp>
#include <mbgl/util/containers.hpp>

namespace mbgl {
Expand All @@ -25,12 +24,9 @@ class ShaderGroupGL final : public gfx::ShaderGroup {
constexpr auto& vert = shaders::ShaderSource<ShaderID, gfx::Backend::Type::OpenGL>::vertex;
constexpr auto& frag = shaders::ShaderSource<ShaderID, gfx::Backend::Type::OpenGL>::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<gl::ShaderProgramGL>(shaderName);
if (shader) {
return shader;
Expand Down
4 changes: 1 addition & 3 deletions include/mbgl/shaders/mtl/shader_group.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,7 @@ class ShaderGroup final : public ShaderGroupBase {
constexpr auto& fragMain = ShaderSource::fragmentMainFunction;
constexpr auto permutations = shaders::ShaderSource<ShaderID, gfx::Backend::Type::Metal>::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<mtl::ShaderProgram>(shaderName);
Expand Down
2 changes: 1 addition & 1 deletion src/mbgl/util/hash.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <typename TIter, typename TKey = detail::TIterVal<TIter>, TKey factor = factor<TKey>()>
TKey order_independent_hash(TIter cur, const TIter end) {
TKey order_independent_hash(std::remove_const_t<TIter> cur, const TIter end) {
detail::TIterVal<TIter> sum = 0, product = 1;
for (; cur != end; ++cur) {
const auto& value = *cur;
Expand Down

0 comments on commit f7666f4

Please sign in to comment.