diff --git a/src/ComputeSharp.Shaders/Translation/ShaderLoader.cs b/src/ComputeSharp.Shaders/Translation/ShaderLoader.cs index acdeac27..40859c0d 100644 --- a/src/ComputeSharp.Shaders/Translation/ShaderLoader.cs +++ b/src/ComputeSharp.Shaders/Translation/ShaderLoader.cs @@ -103,6 +103,8 @@ namespace ComputeSharp.Shaders.Translation { if (FieldsInfo.Count > 0) throw new InvalidOperationException("Shader fields already loaded"); + if (ShaderFields.Any(fieldInfo => fieldInfo.IsStatic)) throw new InvalidOperationException("Empty shader body"); + List descriptorRanges = new List(); List readWriteBuffers = new List(); int readWriteBuffersCount = 0; diff --git a/src/ComputeSharp.sln b/src/ComputeSharp.sln index 56d82d29..daaf3fd2 100644 --- a/src/ComputeSharp.sln +++ b/src/ComputeSharp.sln @@ -18,6 +18,10 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "_SolutionItems", "_Solution EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Samples", "Samples", "{0ED8F632-5E17-46BE-8CC3-B14A82D4AEB1}" EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tests", "Tests", "{F8EFBB27-4EE2-4463-A75B-7EFDFB55D0F7}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ComputeSharp.NetCore.Tests", "..\tests\ComputeSharp.NetCore.Tests\ComputeSharp.NetCore.Tests.csproj", "{E91F4D24-922A-4921-A8D7-9C45A143C45D}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -58,12 +62,21 @@ Global {7A48A85D-8866-4DED-A404-653ED09BF396}.Release|Any CPU.Build.0 = Release|Any CPU {7A48A85D-8866-4DED-A404-653ED09BF396}.Release|x64.ActiveCfg = Release|Any CPU {7A48A85D-8866-4DED-A404-653ED09BF396}.Release|x64.Build.0 = Release|Any CPU + {E91F4D24-922A-4921-A8D7-9C45A143C45D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {E91F4D24-922A-4921-A8D7-9C45A143C45D}.Debug|Any CPU.Build.0 = Debug|Any CPU + {E91F4D24-922A-4921-A8D7-9C45A143C45D}.Debug|x64.ActiveCfg = Debug|Any CPU + {E91F4D24-922A-4921-A8D7-9C45A143C45D}.Debug|x64.Build.0 = Debug|Any CPU + {E91F4D24-922A-4921-A8D7-9C45A143C45D}.Release|Any CPU.ActiveCfg = Release|Any CPU + {E91F4D24-922A-4921-A8D7-9C45A143C45D}.Release|Any CPU.Build.0 = Release|Any CPU + {E91F4D24-922A-4921-A8D7-9C45A143C45D}.Release|x64.ActiveCfg = Release|Any CPU + {E91F4D24-922A-4921-A8D7-9C45A143C45D}.Release|x64.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE EndGlobalSection GlobalSection(NestedProjects) = preSolution {7A48A85D-8866-4DED-A404-653ED09BF396} = {0ED8F632-5E17-46BE-8CC3-B14A82D4AEB1} + {E91F4D24-922A-4921-A8D7-9C45A143C45D} = {F8EFBB27-4EE2-4463-A75B-7EFDFB55D0F7} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {4664C5E3-0340-4E22-BCFD-98AAEDF5F2DC} diff --git a/src/ComputeSharp/ComputeSharp.csproj b/src/ComputeSharp/ComputeSharp.csproj index fea65bb6..9cb9e6f1 100644 --- a/src/ComputeSharp/ComputeSharp.csproj +++ b/src/ComputeSharp/ComputeSharp.csproj @@ -14,6 +14,7 @@ https://github.com/Sergio0694/ComputeSharp https://github.com/Sergio0694/ComputeSharp dotnet net netcore netstandard csharp library graphics shader hlsl compute gpu + https://i.imgur.com/oo8dA0q.png diff --git a/tests/ComputeSharp.NetCore.Tests/BufferTests.cs b/tests/ComputeSharp.NetCore.Tests/BufferTests.cs new file mode 100644 index 00000000..fea3f1ed --- /dev/null +++ b/tests/ComputeSharp.NetCore.Tests/BufferTests.cs @@ -0,0 +1,36 @@ +using System; +using ComputeSharp.Graphics.Buffers; +using ComputeSharp.NetCore.Tests.Extensions; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace ComputeSharp.NetCore.Tests +{ + [TestClass] + [TestCategory("Buffers")] + public class BufferTests + { + [TestMethod] + public void ReadWriteBufferGetSetData() + { + float[] array = new float[4096]; + array.AsSpan().FillRandom(); + + using ReadWriteBuffer buffer = Gpu.Default.AllocateReadWriteBuffer(array); + float[] result = buffer.GetData(); + + Assert.IsTrue(array.AsSpan().ContentEquals(result)); + } + + [TestMethod] + public void ReadOnlyBufferGetSetData() + { + float[] array = new float[4096]; + array.AsSpan().FillRandom(); + + using ReadOnlyBuffer buffer = Gpu.Default.AllocateConstantBuffer(array); + float[] result = buffer.GetData(); + + Assert.IsTrue(array.AsSpan().ContentEquals(result)); + } + } +} diff --git a/tests/ComputeSharp.NetCore.Tests/ComputeSharp.NetCore.Tests.csproj b/tests/ComputeSharp.NetCore.Tests/ComputeSharp.NetCore.Tests.csproj new file mode 100644 index 00000000..32d0c982 --- /dev/null +++ b/tests/ComputeSharp.NetCore.Tests/ComputeSharp.NetCore.Tests.csproj @@ -0,0 +1,19 @@ + + + + netcoreapp3.0 + + false + + + + + + + + + + + + + diff --git a/tests/ComputeSharp.NetCore.Tests/Extensions/SpanExtensions.cs b/tests/ComputeSharp.NetCore.Tests/Extensions/SpanExtensions.cs new file mode 100644 index 00000000..bb52f8be --- /dev/null +++ b/tests/ComputeSharp.NetCore.Tests/Extensions/SpanExtensions.cs @@ -0,0 +1,54 @@ +using System; +using System.Diagnostics.Contracts; +using System.Runtime.CompilerServices; + +namespace ComputeSharp.NetCore.Tests.Extensions +{ + /// + /// An helper with methods to process fixed-size buffers + /// + public static class SpanExtensions + { + /// + /// Fills an input instance with random values + /// + /// The target to fill + public static void FillRandom(this Span span) + { + if (span.Length == 0) return; + + var l = span.Length; + ref var rspan = ref span.GetPinnableReference(); + + for (var i = 0; i < l; i++) + Unsafe.Add(ref rspan, i) = (float)ConcurrentRandom.Instance.NextDouble(); + } + + /// + /// The maximum threshold to validate two values as being equal + /// + private const float FloatThreshold = 0.00001f; + + /// + /// Checks whether or not the two input instances have the same content + /// + /// The first to check + /// The second to check + [Pure] + public static bool ContentEquals(this Span a, Span b) + { + if (a.Length != b.Length) return false; + if (a.Length == 0) return true; + + var l = a.Length; + ref var ra = ref a.GetPinnableReference(); + ref var rb = ref b.GetPinnableReference(); + + for (var i = 0; i < l; i++) + if (Math.Abs(Unsafe.Add(ref ra, i) - Unsafe.Add(ref rb, i)) > FloatThreshold) + return false; + + return true; + } + } +} diff --git a/tests/ComputeSharp.NetCore.Tests/Extensions/Types/ConcurrentRandom.cs b/tests/ComputeSharp.NetCore.Tests/Extensions/Types/ConcurrentRandom.cs new file mode 100644 index 00000000..5cbd72c0 --- /dev/null +++ b/tests/ComputeSharp.NetCore.Tests/Extensions/Types/ConcurrentRandom.cs @@ -0,0 +1,28 @@ +using System.Runtime.CompilerServices; +using System.Threading; + +namespace System +{ + /// + /// A thread-safe wrapper class + /// + public static class ConcurrentRandom + { + // Shared seed + private static int _Seed = Environment.TickCount; + + /// + /// The object that provides the instances + /// + private static readonly ThreadLocal _Instance = new ThreadLocal(() => new Random(Interlocked.Increment(ref _Seed))); + + /// + /// Gets a thread-safe instance + /// + public static Random Instance + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + get => _Instance.Value; + } + } +} \ No newline at end of file diff --git a/tests/ComputeSharp.NetCore.Tests/InvalidShaderTests.cs b/tests/ComputeSharp.NetCore.Tests/InvalidShaderTests.cs new file mode 100644 index 00000000..a0693718 --- /dev/null +++ b/tests/ComputeSharp.NetCore.Tests/InvalidShaderTests.cs @@ -0,0 +1,19 @@ +using System; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace ComputeSharp.NetCore.Tests +{ + [TestClass] + [TestCategory("InvalidShader")] + public class InvalidShaderTests + { + [TestMethod] + public void EmptyShaderCompileError() + { + Action action = id => { }; + + Assert.ThrowsException(() => Gpu.Default.For(10, action)); + } + } +} + diff --git a/tests/ComputeSharp.NetCore.Tests/ShaderClosureTests.cs b/tests/ComputeSharp.NetCore.Tests/ShaderClosureTests.cs new file mode 100644 index 00000000..3220a518 --- /dev/null +++ b/tests/ComputeSharp.NetCore.Tests/ShaderClosureTests.cs @@ -0,0 +1,85 @@ +using System; +using System.Linq; +using System.Numerics; +using ComputeSharp.Graphics.Buffers; +using ComputeSharp.NetCore.Tests.Extensions; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace ComputeSharp.NetCore.Tests +{ + [TestClass] + [TestCategory("ShaderClosure")] + public class ShaderClosureTests + { + [TestMethod] + public void LocalScalarAssignToBuffer() + { + float value = 10; + using ReadWriteBuffer buffer = Gpu.Default.AllocateReadWriteBuffer(1); + + Action action = id => buffer[0] = value; + + Gpu.Default.For(1, action); + + float[] result = buffer.GetData(); + + Assert.IsTrue((int)result[0] == (int)value); + } + + [TestMethod] + public void LocalKnownVectorAssignToBuffer() + { + Vector4 vector4 = new Vector4(1, 2, 3, 4); + using ReadWriteBuffer buffer = Gpu.Default.AllocateReadWriteBuffer(4); + + Action action = id => + { + if (id.X == 0) buffer[0] = vector4.X; + else if (id.X == 1) buffer[1] = vector4.Y; + else if (id.X == 2) buffer[2] = vector4.Z; + else if (id.X == 3) buffer[3] = vector4.W; + }; + + Gpu.Default.For(1, action); + + float[] result = buffer.GetData(); + float[] expected = { 1, 2, 3, 4 }; + + Assert.IsTrue(result.AsSpan().ContentEquals(expected)); + } + + [TestMethod] + public void CopyBetweenWriteableBuffers() + { + float[] source = Enumerable.Range(0, 100).Select(i => (float)i).ToArray(); + + using ReadWriteBuffer input = Gpu.Default.AllocateReadWriteBuffer(source); + using ReadWriteBuffer buffer = Gpu.Default.AllocateReadWriteBuffer(source.Length); + + Action action = id => buffer[id.X] = input[id.X]; + + Gpu.Default.For(source.Length, action); + + float[] result = buffer.GetData(); + + Assert.IsTrue(result.AsSpan().ContentEquals(source)); + } + + [TestMethod] + public void CopyBetweenReadOnlyAndWriteableBuffers() + { + float[] source = Enumerable.Range(0, 100).Select(i => (float)i).ToArray(); + + using ReadOnlyBuffer input = Gpu.Default.AllocateConstantBuffer(source); + using ReadWriteBuffer buffer = Gpu.Default.AllocateReadWriteBuffer(source.Length); + + Action action = id => buffer[id.X] = input[id.X]; + + Gpu.Default.For(source.Length, action); + + float[] result = buffer.GetData(); + + Assert.IsTrue(result.AsSpan().ContentEquals(source)); + } + } +} diff --git a/tests/ComputeSharp.NetCore.Tests/ShaderDispatchTests.cs b/tests/ComputeSharp.NetCore.Tests/ShaderDispatchTests.cs new file mode 100644 index 00000000..7e912983 --- /dev/null +++ b/tests/ComputeSharp.NetCore.Tests/ShaderDispatchTests.cs @@ -0,0 +1,58 @@ +using System; +using System.Linq; +using ComputeSharp.Graphics.Buffers; +using ComputeSharp.NetCore.Tests.Extensions; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace ComputeSharp.NetCore.Tests +{ + [TestClass] + [TestCategory("ShaderDispatch")] + public class ShaderDispatch + { + [TestMethod] + public void WriteToReadWriteBufferDispatch1D() + { + using ReadWriteBuffer buffer = Gpu.Default.AllocateReadWriteBuffer(100); + + Action action = id => buffer[id.X] = id.X; + + Gpu.Default.For(100, action); + + float[] array = buffer.GetData(); + float[] expected = Enumerable.Range(0, 100).Select(i => (float)i).ToArray(); + + Assert.IsTrue(array.AsSpan().ContentEquals(expected)); + } + + [TestMethod] + public void WriteToReadWriteBufferDispatch2D() + { + using ReadWriteBuffer buffer = Gpu.Default.AllocateReadWriteBuffer(100); + + Action action = id => buffer[id.X + id.Y * 10] = id.X + id.Y * 10; + + Gpu.Default.For(10, 10, action); + + float[] array = buffer.GetData(); + float[] expected = Enumerable.Range(0, 100).Select(i => (float)i).ToArray(); + + Assert.IsTrue(array.AsSpan().ContentEquals(expected)); + } + + [TestMethod] + public void WriteToReadWriteBufferDispatch3D() + { + using ReadWriteBuffer buffer = Gpu.Default.AllocateReadWriteBuffer(1000); + + Action action = id => buffer[id.X + id.Y * 10 + id.Z * 100] = id.X + id.Y * 10 + id.Z * 100; + + Gpu.Default.For(10, 10, 10, action); + + float[] array = buffer.GetData(); + float[] expected = Enumerable.Range(0, 1000).Select(i => (float)i).ToArray(); + + Assert.IsTrue(array.AsSpan().ContentEquals(expected)); + } + } +}