Improve the sampling routine cache key hash
On 64-bit platforms more collisions can be prevented by spreading
the three 32-bit values more widely apart.
Bug: b/137649247
Change-Id: Icf429b4fc5a3d5d4472b346b190e48ac429bf881
Reviewed-on: https://swiftshader-review.googlesource.com/c/SwiftShader/+/34929
Presubmit-Ready: Nicolas Capens <nicolascapens@google.com>
Reviewed-by: Ben Clayton <bclayton@google.com>
Kokoro-Presubmit: kokoro <noreply+kokoro@google.com>
Tested-by: Nicolas Capens <nicolascapens@google.com>
diff --git a/src/Vulkan/VkDevice.hpp b/src/Vulkan/VkDevice.hpp
index ae7c21f..809ef2f 100644
--- a/src/Vulkan/VkDevice.hpp
+++ b/src/Vulkan/VkDevice.hpp
@@ -122,10 +122,12 @@
inline std::size_t vk::Device::SamplingRoutineCache::Key::Hash::operator() (const Key& key) const noexcept
{
- auto hash = key.instruction;
- hash = (hash * 31) ^ key.sampler;
- hash = (hash * 31) ^ key.imageView;
- return hash;
+ // Combine three 32-bit integers into a 64-bit hash.
+ // 2642239 is the largest prime which when cubed is smaller than 2^64.
+ uint64_t hash = key.instruction;
+ hash = (hash * 2642239) ^ key.sampler;
+ hash = (hash * 2642239) ^ key.imageView;
+ return static_cast<std::size_t>(hash); // Truncates to 32-bits on 32-bit platforms.
}
} // namespace vk