From c2bd62d86eee75433e1c3df4f30b73e439e717f2 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 4 Sep 2022 15:32:11 +0200 Subject: [PATCH] Add global shader method test --- ...ComputeSharp.Tests.GlobalStatements.csproj | 4 ++ .../Program.cs | 40 ++++++++++++++++++- 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/tests/ComputeSharp.Tests.GlobalStatements/ComputeSharp.Tests.GlobalStatements.csproj b/tests/ComputeSharp.Tests.GlobalStatements/ComputeSharp.Tests.GlobalStatements.csproj index 5fa7cd02..ca713b52 100644 --- a/tests/ComputeSharp.Tests.GlobalStatements/ComputeSharp.Tests.GlobalStatements.csproj +++ b/tests/ComputeSharp.Tests.GlobalStatements/ComputeSharp.Tests.GlobalStatements.csproj @@ -6,6 +6,10 @@ AnyCPU;x64;ARM64 enable + + + + diff --git a/tests/ComputeSharp.Tests.GlobalStatements/Program.cs b/tests/ComputeSharp.Tests.GlobalStatements/Program.cs index 3751555c..a9507482 100644 --- a/tests/ComputeSharp.Tests.GlobalStatements/Program.cs +++ b/tests/ComputeSharp.Tests.GlobalStatements/Program.cs @@ -1,2 +1,38 @@ -// See https://aka.ms/new-console-template for more information -Console.WriteLine("Hello, World!"); +using ComputeSharp; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +float[] numbers = Enumerable.Range(0, 128).Select(i => (float)i).ToArray(); + +using GraphicsDevice device = GraphicsDevice.GetDefault(); +using ReadWriteBuffer buffer = device.AllocateReadWriteBuffer(numbers); + +device.For(buffer.Length, new ApplyFunctionShader(buffer, AddHalf)); + +foreach (ref float number in numbers.AsSpan()) +{ + number = AddHalf(number); +} + +float[] result = buffer.ToArray(); + +CollectionAssert.AreEqual( + expected: numbers, + actual: result); + +Console.WriteLine("Test passed successfully"); + +// See https://github.com/Sergio0694/ComputeSharp/issues/374 +[ShaderMethod] +static float AddHalf(float input) => input + 0.5f; + +[AutoConstructor] +public readonly partial struct ApplyFunctionShader : IComputeShader +{ + private readonly ReadWriteBuffer sourceTexture; + private readonly Func func; + + public void Execute() + { + this.sourceTexture[ThreadIds.X] = this.func(this.sourceTexture[ThreadIds.X]); + } +} \ No newline at end of file