Remove IShader.GetDispatchId() infrastructure

このコミットが含まれているのは:
Sergio Pedri committed 2023-10-13 10:58:57 +02:00
1 親 9acb650627
コミット 3db275a157
19ファイル変更
+14 -709

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

@@ -10,7 +10,6 @@
<ProjectReference Include="..\..\src\ComputeSharp.Core\ComputeSharp.Core.csproj" />
<ProjectReference Include="..\..\src\ComputeSharp.Core.SourceGenerators\ComputeSharp.Core.SourceGenerators.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" PrivateAssets="contentfiles;build" />
<ProjectReference Include="..\..\src\ComputeSharp\ComputeSharp.csproj" />
<ProjectReference Include="..\..\src\ComputeSharp.Dynamic\ComputeSharp.Dynamic.csproj" />
<ProjectReference Include="..\..\src\ComputeSharp.SourceGenerators\ComputeSharp.SourceGenerators.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" PrivateAssets="contentfiles;build" />
</ItemGroup>
</Project>
+9 -8
ファイルの表示
@@ -5,11 +5,11 @@ float[] numbers = Enumerable.Range(0, 128).Select(i => (float)i).ToArray();
using GraphicsDevice device = GraphicsDevice.GetDefault();
using ReadWriteBuffer<float> buffer = device.AllocateReadWriteBuffer(numbers);
device.For(buffer.Length, new ApplyFunctionShader(buffer, AddHalf));
device.For(buffer.Length, new ApplyFunctionShader(buffer));
foreach (ref float number in numbers.AsSpan())
{
number = AddHalf(number);
number = ApplyFunctionShader.AddHalf(number);
}
float[] result = buffer.ToArray();
@@ -21,18 +21,19 @@ if (!numbers.SequenceEqual(result))
Console.WriteLine("Test passed successfully!");
// See https://github.com/Sergio0694/ComputeSharp/issues/374
[ShaderMethod]
static float AddHalf(float input) => input + 0.5f;
[AutoConstructor]
[EmbeddedBytecode(DispatchAxis.X)]
public readonly partial struct ApplyFunctionShader : IComputeShader
{
private readonly ReadWriteBuffer<float> sourceTexture;
private readonly Func<float, float> func;
public static float AddHalf(float input)
{
return input + 0.5f;
}
public void Execute()
{
this.sourceTexture[ThreadIds.X] = this.func(this.sourceTexture[ThreadIds.X]);
this.sourceTexture[ThreadIds.X] = AddHalf(this.sourceTexture[ThreadIds.X]);
}
}
-78
ファイルの表示
@@ -1,78 +0,0 @@
using System;
using ComputeSharp.__Internals;
using Microsoft.VisualStudio.TestTools.UnitTesting;
#pragma warning disable CS0618
namespace ComputeSharp.Tests.Internals;
[TestClass]
[TestCategory("ShaderHashCode")]
public partial class ShaderHashCodeTests
{
public partial struct Shader1 : IComputeShader
{
public float A;
public ReadWriteBuffer<float> B;
public void Execute()
{
this.B[0] = this.A;
}
}
[TestMethod]
public void ShaderWithNoCapturedDelegates()
{
float value = 10;
using ReadWriteBuffer<float> buffer = GraphicsDevice.GetDefault().AllocateReadWriteBuffer<float>(1);
Shader1 shader1 = new() { A = value, B = buffer };
int hash1 = ((IShader)shader1).GetDispatchId();
int hash2 = ((IShader)shader1).GetDispatchId();
Assert.IsTrue(hash1 == hash2);
Shader1 shader2 = new() { A = value, B = buffer };
int hash3 = ((IShader)shader2).GetDispatchId();
Assert.IsTrue(hash1 == hash3);
}
public partial struct Shader2 : IComputeShader
{
public float A;
public ReadWriteBuffer<float> B;
public Func<float, float> F;
public void Execute()
{
this.B[0] = this.F(this.A);
}
}
[TestMethod]
public void ShaderWithCapturedDelegates()
{
Func<float, float> f = static x => x * x;
using ReadWriteBuffer<float> buffer = GraphicsDevice.GetDefault().AllocateReadWriteBuffer<float>(1);
Shader2 shader1 = new() { A = 1, B = buffer, F = f };
int hash1 = ((IShader)shader1).GetDispatchId();
int hash2 = ((IShader)shader1).GetDispatchId();
Assert.IsTrue(hash1 == hash2);
f = static x => x + 1;
Shader2 shader2 = new() { A = 1, B = buffer, F = f };
int hash3 = ((IShader)shader2).GetDispatchId();
Assert.IsFalse(hash1 == hash3);
}
}
-35
ファイルの表示
@@ -1481,41 +1481,6 @@ public class DiagnosticsTests
VerifyGeneratedDiagnostics<IShaderGenerator>(source, "CMPS0042", "CMPS0047");
}
[TestMethod]
public void EmbeddedBytecodeWithDynamicShader()
{
const string source = """
using System;
using ComputeSharp;
namespace ComputeSharp
{
public interface IComputeShader { }
public class ReadWriteBuffer<T> { }
public class EmbeddedBytecodeAttribute : Attribute
{
public EmbeddedBytecodeAttribute(int threadsX, int threadsY, int threadsZ) { }
}
}
namespace MyFancyApp.Sample
{
[EmbeddedBytecode(8, 8, 1)]
public struct MyShader : IComputeShader
{
public ReadWriteBuffer<float> buffer;
public Action action;
public void Execute()
{
}
}
}
""";
VerifyGeneratedDiagnostics<IShaderGenerator>(source, "CMPS0043");
}
[TestMethod]
[DataRow(-1, 1, 1)]
[DataRow(1, -1, 1)]
-251
ファイルの表示
@@ -1,251 +0,0 @@
using System;
using ComputeSharp.Tests.Attributes;
using ComputeSharp.Tests.Extensions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
#pragma warning disable IDE0062
namespace ComputeSharp.Tests;
[TestClass]
[TestCategory("Metaprogramming")]
public partial class MetaprogrammingTests
{
[AutoConstructor]
internal readonly partial struct ActivationWithDelegateShader : IComputeShader
{
public readonly ReadWriteBuffer<int> buffer;
public readonly Func<int, int> activation;
public void Execute()
{
this.buffer[ThreadIds.X] = this.activation(ThreadIds.X);
}
}
public static class Activations
{
[ShaderMethod]
public static int Square(int x)
{
return x * x;
}
[ShaderMethod]
public static int AddOne(int x)
{
return x + 1;
}
// Missing attribute
public static int Identity(int x)
{
return x;
}
[ShaderMethod]
public static int FunctionWithConstants(int x)
{
return (int)(x + FourAndAHalf);
}
[ShaderMethod]
public static Position FunctionWithTypes(int x)
{
Point point = default;
point.A = x;
point.B = x * x;
Position position = default;
position.X = point.A;
position.Y = point.B;
return position;
}
public struct Point
{
public int A;
public int B;
}
[ShaderMethod]
public static int FunctionWithLocalFunctionAndStaticMethod(int x)
{
static int AddTwo(int x) => x + 2;
return Square(3 + AddOne(x) + AddTwo(x));
}
}
[CombinatorialTestMethod]
[AllDevices]
public unsafe void ActivationWithDelegate(Device device)
{
using ReadWriteBuffer<int> buffer = device.Get().AllocateReadWriteBuffer<int>(100);
device.Get().For(100, new ActivationWithDelegateShader(buffer, Activations.Square));
int[] result = buffer.ToArray();
for (int i = 0; i < 100; i++)
{
Assert.AreEqual(result[i], Activations.Square(i));
}
device.Get().For(100, new ActivationWithDelegateShader(buffer, Activations.AddOne));
result = buffer.ToArray();
for (int i = 0; i < 100; i++)
{
Assert.AreEqual(result[i], Activations.AddOne(i));
}
}
[CombinatorialTestMethod]
[AllDevices]
[ExpectedException(typeof(ArgumentException))]
public unsafe void DelegateWithMissingAttributeOnMethod(Device device)
{
using ReadWriteBuffer<int> buffer = device.Get().AllocateReadWriteBuffer<int>(100);
device.Get().For(100, new ActivationWithDelegateShader(buffer, Activations.Identity));
}
[CombinatorialTestMethod]
[AllDevices]
[ExpectedException(typeof(ArgumentException))]
public unsafe void DelegateWrappingNonStaticMethod(Device device)
{
using ReadWriteBuffer<int> buffer = device.Get().AllocateReadWriteBuffer<int>(100);
[ShaderMethod]
int Dummy(int x) => x;
device.Get().For(100, new ActivationWithDelegateShader(buffer, Dummy));
}
[AutoConstructor]
internal readonly partial struct ActivationWithMultipleDelegatesShader : IComputeShader
{
public readonly ReadWriteBuffer<int> buffer;
public readonly Func<int, int> f1;
public readonly Func<int, int> f2;
public void Execute()
{
this.buffer[ThreadIds.X] = this.f2(this.f1(ThreadIds.X));
}
}
[CombinatorialTestMethod]
[AllDevices]
public unsafe void ActivationWithMultipleDelegates(Device device)
{
using ReadWriteBuffer<int> buffer = device.Get().AllocateReadWriteBuffer<int>(100);
device.Get().For(100, new ActivationWithMultipleDelegatesShader(buffer, Activations.Square, Activations.AddOne));
int[] result = buffer.ToArray();
for (int i = 0; i < 100; i++)
{
Assert.AreEqual(result[i], Activations.AddOne(Activations.Square(i)));
}
}
public const float FourAndAHalf = 4.5f;
public const int MagicNumber = 42;
[AutoConstructor]
internal readonly partial struct DelegateWithConstantsShader : IComputeShader
{
public readonly ReadWriteBuffer<int> buffer;
public readonly Func<int, int> activation;
public void Execute()
{
this.buffer[ThreadIds.X] = MagicNumber + this.activation(ThreadIds.X);
}
}
[CombinatorialTestMethod]
[AllDevices]
public unsafe void DelegateWithConstants(Device device)
{
using ReadWriteBuffer<int> buffer = device.Get().AllocateReadWriteBuffer<int>(100);
device.Get().For(100, new DelegateWithConstantsShader(buffer, Activations.FunctionWithConstants));
int[] result = buffer.ToArray();
for (int i = 0; i < 100; i++)
{
Assert.AreEqual(result[i], MagicNumber + Activations.FunctionWithConstants(i));
}
}
public struct Position
{
public int X;
public int Y;
}
[AutoConstructor]
internal readonly partial struct DelegateWithTypesShader : IComputeShader
{
public readonly ReadWriteBuffer<Position> buffer;
public readonly Func<int, Position> activation;
public void Execute()
{
this.buffer[ThreadIds.X] = this.activation(ThreadIds.X);
}
}
[CombinatorialTestMethod]
[AllDevices]
public unsafe void DelegateWithTypes(Device device)
{
using ReadWriteBuffer<Position> buffer = device.Get().AllocateReadWriteBuffer<Position>(100);
device.Get().For(100, new DelegateWithTypesShader(buffer, Activations.FunctionWithTypes));
Position[] result = buffer.ToArray();
for (int i = 0; i < 100; i++)
{
Assert.AreEqual(result[i].X, Activations.FunctionWithTypes(i).X);
Assert.AreEqual(result[i].Y, Activations.FunctionWithTypes(i).Y);
}
}
[AutoConstructor]
internal readonly partial struct DelegateWithLocalFunctionsAndStaticMethodsShader : IComputeShader
{
public readonly ReadWriteBuffer<int> buffer;
public readonly Func<int, int> activation;
public void Execute()
{
this.buffer[ThreadIds.X] = Activations.AddOne(ThreadIds.X) + this.activation(ThreadIds.X);
}
}
[CombinatorialTestMethod]
[AllDevices]
public unsafe void DelegateWithLocalFunctionsAndStaticMethods(Device device)
{
using ReadWriteBuffer<int> buffer = device.Get().AllocateReadWriteBuffer<int>(100);
device.Get().For(100, new DelegateWithLocalFunctionsAndStaticMethodsShader(buffer, Activations.FunctionWithLocalFunctionAndStaticMethod));
int[] result = buffer.ToArray();
for (int i = 0; i < 100; i++)
{
Assert.AreEqual(result[i], Activations.AddOne(i) + Activations.FunctionWithLocalFunctionAndStaticMethod(i));
}
}
}