Improved cache system for shaders with captured delegates

このコミットが含まれているのは:
Sergio Pedri committed 2019-09-05 15:11:03 +02:00
1 親 2c44c8fdbd
コミット 146b98ea89
2ファイル変更
+26 -4

検索条件に一致するファイルはありません

+2 -3
ファイルの表示
@@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using ComputeSharp.Graphics;
using ComputeSharp.Graphics.Buffers.Abstract;
using ComputeSharp.Graphics.Buffers.Extensions;
@@ -41,7 +40,7 @@ namespace ComputeSharp.Shaders
/// <summary>
/// The mapping used to cache and reuse compiled shaders
/// </summary>
private static readonly Dictionary<(MethodInfo Method, int ThreadsX, int ThreadsY, int ThreadsZ), (ShaderLoader Loader, ShaderBytecode Bytecode)> ShadersCache = new Dictionary<(MethodInfo, int, int, int), (ShaderLoader, ShaderBytecode)>();
private static readonly Dictionary<(int Id, int ThreadsX, int ThreadsY, int ThreadsZ), (ShaderLoader Loader, ShaderBytecode Bytecode)> ShadersCache = new Dictionary<(int, int, int, int), (ShaderLoader, ShaderBytecode)>();
/// <summary>
/// Compiles and runs the input shader on a target <see cref="GraphicsDevice"/> instance, with the specified parameters
@@ -61,7 +60,7 @@ namespace ComputeSharp.Shaders
Action<ThreadIds> action)
{
// Try to get the cache shader
var key = (action.Method, threadsX, threadsY, threadsZ);
var key = (ShaderLoader.GetHashCode(action), threadsX, threadsY, threadsZ);
if (!ShadersCache.TryGetValue(key, out var shaderData))
{
// Load the input shader
+24 -1
ファイルの表示
@@ -148,6 +148,30 @@ namespace ComputeSharp.Shaders.Translation
/// </summary>
public IReadOnlyList<FunctionInfo> FunctionsList => _FunctionsList;
/// <summary>
/// Gets a unique hash code for a given <see cref="Action{T}"/>
/// </summary>
/// <param name="action">The input <see cref="Action{T}"/> instance to inspect</param>
[Pure]
public static int GetHashCode(Action<ThreadIds> action)
{
int hashcode = action.Method.GetHashCode();
foreach (FieldInfo fieldInfo in action.Method.DeclaringType.GetFields())
{
if (fieldInfo.FieldType.IsClass && fieldInfo.FieldType.IsGenericType &&
fieldInfo.GetValue(action.Target) is Delegate func && func.Method.IsStatic &&
(HlslKnownTypes.IsKnownScalarType(func.Method.ReturnType) || HlslKnownTypes.IsKnownVectorType(func.Method.ReturnType)) &&
fieldInfo.FieldType.GenericTypeArguments.All(type => HlslKnownTypes.IsKnownScalarType(type) ||
HlslKnownTypes.IsKnownVectorType(type)))
{
hashcode = unchecked(hashcode * 17 + func.Method.GetHashCode());
}
}
return hashcode;
}
/// <summary>
/// Loads and processes an input <see cref="Action{T}"/>
/// </summary>
@@ -247,7 +271,6 @@ namespace ComputeSharp.Shaders.Translation
}
}
else if (fieldType.IsClass && fieldType.IsGenericType &&
fieldType.IsSubclassOf(typeof(Delegate)) &&
memberInfo.GetValue(Action.Target) is Delegate func && func.Method.IsStatic &&
(HlslKnownTypes.IsKnownScalarType(func.Method.ReturnType) || HlslKnownTypes.IsKnownVectorType(func.Method.ReturnType)) &&
fieldType.GenericTypeArguments.All(type => HlslKnownTypes.IsKnownScalarType(type) ||