浏览代码

(wip) added hash utilities

/feature-ReflectionProbeBaking
Frédéric Vauchelles 7 年前
当前提交
b495056c
共有 2 个文件被更改,包括 120 次插入6 次删除
  1. 102
      ScriptableRenderPipeline/Core/CoreRP/Hash128Utilities.cs
  2. 24
      Tests/Scripts/Editor/CoreTests/HashUtilitiesTests.cs

102
ScriptableRenderPipeline/Core/CoreRP/Hash128Utilities.cs


using System.Runtime.InteropServices;
namespace UnityEngine.Experimental.Rendering
{

{
var size = sizeof(T);
fixed (void* data = &hashInOut)
fixed (void* message = &value)
var reference = __makeref(value);
void* message = &reference;
var size = Marshal.SizeOf(typeof(T));
ComputeHash128(message, size, ref hashInOut);
}
public static unsafe void ComputeHash128(void* message, int size, ref Hash128 hashInOut)
{
fixed (void* hashData = &hashInOut)
var ptr = (ulong*)data;
var ptr = (ulong*)hashData;
}
public static unsafe Hash128 ComputeHash128<T>(T value)
{
var reference = __makeref(value);
void* message = &reference;
var size = Marshal.SizeOf(typeof(T));
return ComputeHash128(message, size);
}
public static unsafe Hash128 ComputeHash128(void* message, int size)
{
var result = new Hash128();
var ptr = (ulong*)&result;
SpookyHashV2.Hash128(message, size, ptr, ptr + 1);
return result;
}
public static void AppendHash(Hash128 toAdd, ref Hash128 hashInOut)
{
ComputeHash128(toAdd, ref hashInOut);
}
public static Hash128 CombineHashes(Hash128 a, Hash128 b)
{
var result = a;
ComputeHash128(b, ref result);
return result;
}
public static Hash128 CombineHashes(Hash128 a, Hash128 b, Hash128 c)
{
var result = a;
ComputeHash128(b, ref result);
ComputeHash128(c, ref result);
return result;
}
public static Hash128 CombineHashes(Hash128 a, Hash128 b, Hash128 c, Hash128 d)
{
var result = a;
ComputeHash128(b, ref result);
ComputeHash128(c, ref result);
ComputeHash128(d, ref result);
return result;
}
public static Hash128 CombineHashes(Hash128 a, Hash128 b, Hash128 c, Hash128 d, Hash128 e)
{
var result = a;
ComputeHash128(b, ref result);
ComputeHash128(c, ref result);
ComputeHash128(d, ref result);
ComputeHash128(e, ref result);
return result;
}
public static Hash128 CombineHashes(Hash128 a, Hash128 b, Hash128 c, Hash128 d, Hash128 e, Hash128 f)
{
var result = a;
ComputeHash128(b, ref result);
ComputeHash128(c, ref result);
ComputeHash128(d, ref result);
ComputeHash128(e, ref result);
ComputeHash128(f, ref result);
return result;
}
public static unsafe Hash128 QuantizedHash(Matrix4x4 matrix)
{
var quantisedMatrix = stackalloc int[16];
for (var i = 0; i < 16; ++i)
quantisedMatrix[i] = (int)((matrix[i] * 1000) + .5f);
return ComputeHash128(&quantisedMatrix, 16 * sizeof(int));
}
public static unsafe Hash128 QuantizedHash(Vector3 vector)
{
var quantisedVector = stackalloc int[3];
for (var i = 0; i < 3; ++i)
quantisedVector[i] = (int)((vector[i] * 1000) + .5f);
return ComputeHash128(&quantisedVector, 3 * sizeof(int));
}
}
}

24
Tests/Scripts/Editor/CoreTests/HashUtilitiesTests.cs


using NUnit.Framework;
using UnityEngine.Experimental.Rendering;
namespace UnityEditor.Experimental.Rendering.Tests
{

[Test]
void CreateHashTest()
void ComputeHash128()
ComputeHash128_EqualsTest(0, 1);
ComputeHash128_EqualsTest(0u, 1u);
ComputeHash128_EqualsTest(0f, 1f);
ComputeHash128_EqualsTest(0.0, 1.0);
ComputeHash128_EqualsTest("zer", "zerez");
}
static void ComputeHash128_EqualsTest<T>(T a, T b)
{
{
var hash0 = Hash128Utilities.ComputeHash128(a);
var hash1 = Hash128Utilities.ComputeHash128(b);
Assert.AreNotEqual(hash0, hash1, "Different values creates different hashes " + typeof(T));
}
{
var hash0 = Hash128Utilities.ComputeHash128(a);
var hash1 = Hash128Utilities.ComputeHash128(a);
Assert.AreEqual(hash0, hash1, "Same values creates same hashes " + typeof(T));
}
}
}
}
正在加载...
取消
保存