For now, I am Using SpookyHashBuilder from Unity.Physics.
But it’s internal and I have to hack it out.
Is there a more Standard way of building it?
IConvertGameObjectToEntity and GameObjectConversionSystem
Need this hash to add new BlobAsset anyway.
Use xxHash3 class in collections
I did not find it… Do you mean UnityEngine.Hash128?
Are you on latest release? Maybe also only available in the next collections release.
Collections Version 0.12.0-preview.13 - August 14, 2020
Dis not see any newer release
XXHash is hidden away in the entities package. <YOURPROJECT>/Library/PackageCache/com.unity.entities@0.11.0-preview.7/Unity.Core/Hashes/XXHash.cs
. The api is minimal but it has everything needed to be wrapped up nicely for use with native collections. Class XXHash | Entities | 0.11.2-preview.1
I expect this would be an ok start. Warning, untested code, I’m just scribbling this in the code tag, please double-check it
public static unsafe uint Hash<T>(NativeArray<T> arr, uint seed = 0) where T : struct
{
var byteLen = UnsafeUtility.SizeOf<T>() * arr.Length;
return Unity.Core.Hash32(arr.GetUnsafeReadOnlyPtr(), byteLen, seed);
}
Mathematics package already contains static hash methods for many types in math, but also have implementation with direct memory block usage and based on xxhash32.
/// <summary>Returns a uint hash from a block of memory using the xxhash32 algorithm. Can only be used in an unsafe context.</summary>
/// <param name="pBuffer">A pointer to the beginning of the data.</param>
/// <param name="numBytes">Number of bytes to hash.</param>
/// <param name="seed">Starting seed value.</param>
public static unsafe uint hash(void* pBuffer, int numBytes, uint seed = 0)
{
unchecked
{
const uint Prime1 = 2654435761;
const uint Prime2 = 2246822519;
const uint Prime3 = 3266489917;
const uint Prime4 = 668265263;
const uint Prime5 = 374761393;
uint4* p = (uint4*)pBuffer;
uint hash = seed + Prime5;
if (numBytes >= 16)
{
uint4 state = new uint4(Prime1 + Prime2, Prime2, 0, (uint)-Prime1) + seed;
int count = numBytes >> 4;
for (int i = 0; i < count; ++i)
{
state += *p++ * Prime2;
state = (state << 13) | (state >> 19);
state *= Prime1;
}
hash = rol(state.x, 1) + rol(state.y, 7) + rol(state.z, 12) + rol(state.w, 18);
}
hash += (uint)numBytes;
uint* puint = (uint*)p;
for (int i = 0; i < ((numBytes >> 2) & 3); ++i)
{
hash += *puint++ * Prime3;
hash = rol(hash, 17) * Prime4;
}
byte* pbyte = (byte*)puint;
for (int i = 0; i < ((numBytes) & 3); ++i)
{
hash += (*pbyte++) * Prime5;
hash = rol(hash, 11) * Prime1;
}
hash ^= hash >> 15;
hash *= Prime2;
hash ^= hash >> 13;
hash *= Prime3;
hash ^= hash >> 16;
return hash;
}
}
Okay, But still they are all hash32 or hash64. Not directly Hash128 that BlobAssetStore wants.
UnityEngine.Hash128 is still the best choice. Besides, it has to be used in MainThread for the first time. At least Unity.Physic.SpookyHashBuilder said so.
public static unsafe void Initialize()
{
if (s_Initialized) return;
// ensure internal UnityEngine.SpookyHash class is initialized on the main thread before it is used anywhere
var hash128 = new UnityEngine.Hash128();
byte data = 0;
HashUnsafeUtilities.ComputeHash128(UnsafeUtility.AddressOf(ref data), 1, &hash128);
s_Initialized = true;
}
I’am trying to hash a Dictionary and make it a “BlobHashMap” BlobAsset simple data type hash is not very convenient to use.