Added shader hashcode handling for generic types

このコミットが含まれているのは:
Sergio Pedri committed 2020-04-12 16:05:17 +02:00
1 親 fcf7a9a31d
コミット 71213829e7
1ファイル変更
+102
+102
ファイルの表示
@@ -19,6 +19,108 @@ namespace ComputeSharp.Shaders.Translation
/// </summary>
private static readonly Dictionary<int, Hasher?> HasherMapping = new Dictionary<int, Hasher?>();
/// <summary>
/// A type containing static info for a given shader type <typeparamref name="T"/>
/// </summary>
/// <typeparam name="T">The type of compute shader currently in use</typeparam>
private static class TypeInfo<T>
where T : struct, IComputeShader
{
/// <summary>
/// The id of the compute shader type in use
/// </summary>
public static readonly int Id;
/// <summary>
/// The optional <see cref="Hasher{T}"/> instance, if needed
/// </summary>
public static readonly Hasher<T>? Hasher;
/// <summary>
/// Initializes the static members of the <see cref="TypeInfo{T}"/> type
/// </summary>
static TypeInfo()
{
Id = HashCode.Combine(typeof(T));
// Get the valid delegates for delegate checking
FieldInfo[] delegateFieldInfos = (
from fieldInfo in typeof(T).GetFields()
where fieldInfo.FieldType.IsDelegate()
select fieldInfo).ToArray();
// If at least one delegate is present, build the hasher method
if (delegateFieldInfos.Length > 0)
{
Hasher = BuildDynamicHasher<T>(delegateFieldInfos);
}
}
}
/// <summary>
/// Gets a unique hashcode for the input compute shader instance
/// </summary>
/// <param name="shader">The input <typeparamref name="T"/> instance representing the shader to run</param>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int GetHashCode<T>(in T shader)
where T : struct, IComputeShader
{
int hash = TypeInfo<T>.Id;
Hasher<T>? hasher = TypeInfo<T>.Hasher;
if (hasher != null) hash = hasher(hash, shader);
return hash;
}
/// <summary>
/// A <see langword="delegate"/> that represents an aggregate hash function for a given <typeparamref name="T"/> instance
/// </summary>
/// <param name="hash">The initial hash value</param>
/// <param name="shader">The compute shader instance to use to compute the final hash value</param>
/// <returns>The final hash value for the input closure</returns>
private delegate int Hasher<T>(int hash, in T shader)
where T : struct, IComputeShader;
/// <summary>
/// Builds a new <see cref="Hasher"/> instance for the target <see cref="Type"/> and sequence of <see cref="FieldInfo"/> values
/// </summary>
/// <param name="fieldInfos">The list of captured fields to inspect</param>
[Pure]
private static Hasher<T> BuildDynamicHasher<T>(IReadOnlyCollection<FieldInfo> fieldInfos)
where T : struct, IComputeShader
{
return DynamicMethod<Hasher<T>>.New(il =>
{
MethodInfo
getMethodInfo = typeof(Delegate).GetProperty(nameof(Delegate.Method)).GetMethod,
getHashCodeInfo = typeof(object).GetMethod(nameof(GetHashCode));
foreach (FieldInfo fieldInfo in fieldInfos)
{
// (hashcode << 5) + hashcode
il.Emit(OpCodes.Ldarg_0);
il.Emit(OpCodes.Ldc_I4_5);
il.Emit(OpCodes.Shl);
il.Emit(OpCodes.Ldarg_0);
il.Emit(OpCodes.Add);
// hashcode += shader.Function[#].Method.GetHashCode();
il.Emit(OpCodes.Ldarg_1);
il.Emit(OpCodes.Ldfld, fieldInfo);
il.EmitCall(OpCodes.Callvirt, getMethodInfo, null);
il.EmitCall(OpCodes.Callvirt, getHashCodeInfo, null);
il.Emit(OpCodes.Add);
il.Emit(OpCodes.Starg_S, (byte)0);
}
// Return the computed hash
il.Emit(OpCodes.Ldarg_0);
il.Emit(OpCodes.Ret);
});
}
/// <summary>
/// Gets a unique hashcode for the input shader closure
/// </summary>