Removed unnecessary code
このコミットが含まれているのは:
1 親
6d27a2c1c1
コミット
32ca76c535
6ファイル変更
+1
-822
検索条件に一致するファイルはありません
@@ -5,7 +5,6 @@ using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Reflection.Emit;
|
||||
using System.Runtime.CompilerServices;
|
||||
using ComputeSharp.Shaders.Mappings;
|
||||
|
||||
namespace ComputeSharp.Shaders.Translation
|
||||
{
|
||||
@@ -14,11 +13,6 @@ namespace ComputeSharp.Shaders.Translation
|
||||
/// </summary>
|
||||
internal static class ShaderHashCodeProvider
|
||||
{
|
||||
/// <summary>
|
||||
/// The mapping of hashcodes to aggregate hashing functions
|
||||
/// </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>
|
||||
@@ -123,115 +117,5 @@ namespace ComputeSharp.Shaders.Translation
|
||||
il.Emit(OpCodes.Ret);
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a unique hashcode for the input shader closure
|
||||
/// </summary>
|
||||
/// <param name="action">The input <see cref="Action{T}"/> representing the shader to run</param>
|
||||
[Pure]
|
||||
public static int GetHashCode(Action<ThreadIds> 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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A <see langword="delegate"/> that represents an aggregate hash function for a given instance
|
||||
/// </summary>
|
||||
/// <param name="hash">The initial hash value</param>
|
||||
/// <param name="instance">The closure instance to use to compute the final hash value</param>
|
||||
/// <returns>The final hash value for the input closure</returns>
|
||||
private delegate int Hasher(int hash, object instance);
|
||||
|
||||
/// <summary>
|
||||
/// Builds a new <see cref="Hasher"/> instance for the target <see cref="Type"/> and sequence of <see cref="FieldInfo"/> values
|
||||
/// </summary>
|
||||
/// <param name="declaringType">The declaring <see cref="Type"/> to analyze</param>
|
||||
/// <param name="fieldInfos">The list of captured fields to inspect</param>
|
||||
[Pure]
|
||||
private static Hasher BuildDynamicHasher(Type declaringType, IReadOnlyCollection<FieldInfo> fieldInfos)
|
||||
{
|
||||
return DynamicMethod<Hasher>.New(il =>
|
||||
{
|
||||
// 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(object.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.Call, validateDelegateInfo, null);
|
||||
il.Emit(OpCodes.Brfalse_S, label);
|
||||
|
||||
// hash = hash * 17 + field.Method.GetHashCode();
|
||||
il.Emit(OpCodes.Ldarg_0);
|
||||
il.Emit(OpCodes.Ldc_I4_S, (byte)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, (byte)0);
|
||||
|
||||
il.MarkLabel(label);
|
||||
}
|
||||
|
||||
// Return the computed hash
|
||||
il.Emit(OpCodes.Ldarg_0);
|
||||
il.Emit(OpCodes.Ret);
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A method that checks whether an input <see cref="Delegate"/> is supported in HLSL
|
||||
/// </summary>
|
||||
/// <param name="candidate">The input <see cref="Delegate"/> instance to check</param>
|
||||
[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));
|
||||
}
|
||||
}
|
||||
}
|
||||
新しいイシューから参照
ユーザーをブロックする