Merge pull request #48 from Sergio0694/feature_static-functions-support
Added support for captured delegates
このコミットが含まれているのは:
5ファイル変更
+119
-6
検索条件に一致するファイルはありません
@@ -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
|
||||
|
||||
@@ -13,7 +13,6 @@ using ComputeSharp.Shaders.Renderer.Models.Functions;
|
||||
using ComputeSharp.Shaders.Translation.Enums;
|
||||
using ComputeSharp.Shaders.Translation.Models;
|
||||
using Microsoft.CodeAnalysis;
|
||||
using Microsoft.CodeAnalysis.CSharp;
|
||||
using Microsoft.CodeAnalysis.CSharp.Syntax;
|
||||
using Vortice.Direct3D12;
|
||||
using ParameterInfo = ComputeSharp.Shaders.Renderer.Models.Functions.ParameterInfo;
|
||||
@@ -149,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,6 +270,15 @@ namespace ComputeSharp.Shaders.Translation
|
||||
LoadFieldInfo(fieldInfo, null, updatedParents);
|
||||
}
|
||||
}
|
||||
else if (fieldType.IsClass && fieldType.IsGenericType &&
|
||||
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) ||
|
||||
HlslKnownTypes.IsKnownVectorType(type)))
|
||||
{
|
||||
// Captured static delegates with a return type
|
||||
LoadStaticMethodSource(fieldName, func.Method);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -4,7 +4,7 @@ using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
namespace ComputeSharp.Tests
|
||||
{
|
||||
[TestClass]
|
||||
[TestCategory("IntrinsicTests")]
|
||||
[TestCategory("Intrinsics")]
|
||||
public class IntrinsicTests
|
||||
{
|
||||
[TestMethod]
|
||||
|
||||
@@ -0,0 +1,79 @@
|
||||
using System;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
|
||||
namespace ComputeSharp.Tests
|
||||
{
|
||||
[TestClass]
|
||||
[TestCategory("StaticFunctions")]
|
||||
public class StaticFunctionsTests
|
||||
{
|
||||
[TestMethod]
|
||||
public void FloatToFloatFunc()
|
||||
{
|
||||
using ReadWriteBuffer<float> buffer = Gpu.Default.AllocateReadWriteBuffer<float>(1);
|
||||
|
||||
Func<float, float> square = StaticMethodsContainer.Square;
|
||||
|
||||
Gpu.Default.For(1, id => buffer[0] = square(3));
|
||||
|
||||
float[] result = buffer.GetData();
|
||||
|
||||
Assert.IsTrue(MathF.Abs(result[0] - 9) < 0.0001f);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void InternalFloatToFloatFunc()
|
||||
{
|
||||
using ReadWriteBuffer<float> buffer = Gpu.Default.AllocateReadWriteBuffer<float>(1);
|
||||
|
||||
Func<float, float> square = StaticMethodsContainer.InternalSquare;
|
||||
|
||||
Gpu.Default.For(1, id => buffer[0] = square(3));
|
||||
|
||||
float[] result = buffer.GetData();
|
||||
|
||||
Assert.IsTrue(MathF.Abs(result[0] - 9) < 0.0001f);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void ChangingFuncsFromSameMethod()
|
||||
{
|
||||
using ReadWriteBuffer<float> buffer = Gpu.Default.AllocateReadWriteBuffer<float>(1);
|
||||
|
||||
Func<float, float> func = StaticMethodsContainer.Square;
|
||||
|
||||
Gpu.Default.For(1, id => buffer[0] = func(3));
|
||||
|
||||
float[] result = buffer.GetData();
|
||||
|
||||
Assert.IsTrue(MathF.Abs(result[0] - 9) < 0.0001f);
|
||||
|
||||
func = StaticMethodsContainer.Negate;
|
||||
|
||||
Gpu.Default.For(1, id => buffer[0] = func(3));
|
||||
|
||||
result = buffer.GetData();
|
||||
|
||||
Assert.IsTrue(MathF.Abs(result[0] + 3) < 0.0001f);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void ChangingFuncsFromExternalMethod()
|
||||
{
|
||||
MethodRunner(StaticMethodsContainer.Square, 3, 9);
|
||||
|
||||
MethodRunner(StaticMethodsContainer.Negate, 3, -3);
|
||||
}
|
||||
|
||||
private void MethodRunner(Func<float, float> func, float input, float expected)
|
||||
{
|
||||
using ReadWriteBuffer<float> buffer = Gpu.Default.AllocateReadWriteBuffer<float>(1);
|
||||
|
||||
Gpu.Default.For(1, id => buffer[0] = func(input));
|
||||
|
||||
float[] result = buffer.GetData();
|
||||
|
||||
Assert.IsTrue(MathF.Abs(result[0] - expected) < 0.0001f);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -12,6 +12,9 @@ namespace ComputeSharp.Tests
|
||||
[Pure]
|
||||
public static float Square(float x) => x * x;
|
||||
|
||||
[Pure]
|
||||
public static float Negate(float x) => -x;
|
||||
|
||||
[Pure]
|
||||
internal static float InternalSquare(float x) => x * x;
|
||||
|
||||
@@ -27,7 +30,7 @@ namespace ComputeSharp.Tests
|
||||
}
|
||||
|
||||
[TestClass]
|
||||
[TestCategory("StaticMethodsTests")]
|
||||
[TestCategory("StaticMethods")]
|
||||
public class StaticMethodsTests
|
||||
{
|
||||
[TestMethod]
|
||||
|
||||
新しいイシューから参照
ユーザーをブロックする