using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.CompilerServices;
using ComputeSharp.Shaders.Extensions;
using ComputeSharp.Shaders.Mappings;
namespace ComputeSharp.Shaders.Translation
{
///
/// A that handles the cache system for the generated shaders
///
internal static class ShaderHashCodeProvider
{
///
/// The mapping of hashcodes to aggregate hashing functions
///
private static readonly Dictionary HasherMapping = new Dictionary();
///
/// Gets a unique hashcode for the input shader closure
///
/// The input representing the shader to run
[Pure]
public static int GetHashCode(Action action)
{
// Get the delegate hashcode
MethodInfo methodInfo = action.Method;
int hash = methodInfo.GetHashCode();
if (!HasherMapping.TryGetValue(hash, out Hasher? hasher))
{
// Get the candidate fields for delegate checking
FieldInfo[] delegateFieldInfos = (
from fieldInfo in methodInfo.DeclaringType.GetFields()
where fieldInfo.FieldType.IsDelegate() &&
fieldInfo.FieldType.GenericTypeArguments.All(type => HlslKnownTypes.IsKnownScalarType(type) || HlslKnownTypes.IsKnownVectorType(type))
select fieldInfo).ToArray();
// If at least one captured delegate is present, build the hasher method
hasher = delegateFieldInfos.Length == 0 ? null : BuildDynamicHasher(action.Method.DeclaringType, delegateFieldInfos);
HasherMapping.Add(hash, hasher);
}
// Aggregate the hash of the captured delegates, if needed
if (hasher != null) hash = hasher(hash, action.Target);
return hash;
}
///
/// A that represents an aggregate hash function for a given instance
///
/// The source object to get the member from
/// The value of the member, upcast to
private delegate int Hasher(int hash, object instance);
///
/// Builds a new instance for the target and sequence of values
///
/// The declaring to analyze
/// The list of captured fields to inspect
[Pure]
private static Hasher BuildDynamicHasher(Type declaringType, IReadOnlyCollection fieldInfos)
{
// Create a new dynamic method for the current member
DynamicMethod method = new DynamicMethod("GetHashCodeForHlslDelegates", typeof(int), new[] { typeof(int), typeof(object) }, declaringType);
ILGenerator il = method.GetILGenerator();
// Declare the local variable to store the target instance
il.DeclareLocal(declaringType);
// Load the argument (the object instance)
il.Emit(OpCodes.Ldarg_1);
il.Emit(OpCodes.Castclass, declaringType);
il.Emit(OpCodes.Stloc_0);
// Unroll the member access
MethodInfo
validateDelegateInfo = typeof(ShaderHashCodeProvider).GetMethod(nameof(ValidateDelegate), BindingFlags.NonPublic | BindingFlags.Static),
getMethodInfo = typeof(Delegate).GetProperty(nameof(Delegate.Method)).GetMethod,
getHashCodeInfo = typeof(object).GetMethod(nameof(GetHashCode));
foreach (FieldInfo fieldInfo in fieldInfos)
{
Label label = il.DefineLabel();
// if (fieldInfo.GetValue(obj) != null) {
il.Emit(OpCodes.Ldloc_0);
il.Emit(OpCodes.Ldfld, fieldInfo);
il.Emit(OpCodes.Brfalse_S, label);
// if (ValidateDelegate(field)) {
il.Emit(OpCodes.Ldloc_0);
il.Emit(OpCodes.Ldfld, fieldInfo);
il.EmitCall(OpCodes.Callvirt, validateDelegateInfo, null);
il.Emit(OpCodes.Brfalse_S, label);
// hash = hash * 17 + field.Method.GetHashCode();
il.Emit(OpCodes.Ldarg_0);
il.Emit(OpCodes.Ldc_I4, 17);
il.Emit(OpCodes.Mul);
il.Emit(OpCodes.Ldloc_0);
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, 0);
il.MarkLabel(label);
}
// Return the computed hash
il.Emit(OpCodes.Ldarg_0);
il.Emit(OpCodes.Ret);
// Build and return the delegate
return (Hasher)method.CreateDelegate(typeof(Hasher));
}
///
/// A method that checks whether an input is supported in HLSL
///
/// The input instance to check
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static bool ValidateDelegate(Delegate candidate)
{
MethodInfo methodInfo = candidate.Method;
return (methodInfo.IsStatic || methodInfo.DeclaringType.IsStatelessDelegateContainer()) &&
(HlslKnownTypes.IsKnownScalarType(methodInfo.ReturnType) || HlslKnownTypes.IsKnownVectorType(methodInfo.ReturnType));
}
}
}