From 2d200db5ba18b3a63067c1082e3480cf86fa8528 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Thu, 8 Jul 2021 16:39:06 +0200 Subject: [PATCH] Fixed unit test build errors --- .../IComputeShaderHashCodeGenerator.cs | 2 +- .../Helpers/DebugDispatchDataLoader.cs | 56 ++++ .../ShaderDataLoaderTests.cs | 283 ++++++++++-------- .../ShaderHashCodeTests.cs | 21 +- tests/ComputeSharp.Tests/ShadersTests.cs | 11 +- 5 files changed, 235 insertions(+), 138 deletions(-) create mode 100644 tests/ComputeSharp.Tests.Internals/Helpers/DebugDispatchDataLoader.cs diff --git a/src/ComputeSharp.SourceGenerators/IComputeShaderHashCodeGenerator.cs b/src/ComputeSharp.SourceGenerators/IComputeShaderHashCodeGenerator.cs index d8d1abeb..ff0663f9 100644 --- a/src/ComputeSharp.SourceGenerators/IComputeShaderHashCodeGenerator.cs +++ b/src/ComputeSharp.SourceGenerators/IComputeShaderHashCodeGenerator.cs @@ -202,7 +202,7 @@ namespace ComputeSharp.SourceGenerators .AddArgumentListArguments(Argument( MemberAccessExpression( SyntaxKind.SimpleMemberAccessExpression, - IdentifierName("foo"), + IdentifierName(fieldSymbol.Name), IdentifierName("Method")))))); } diff --git a/tests/ComputeSharp.Tests.Internals/Helpers/DebugDispatchDataLoader.cs b/tests/ComputeSharp.Tests.Internals/Helpers/DebugDispatchDataLoader.cs new file mode 100644 index 00000000..60d1ded4 --- /dev/null +++ b/tests/ComputeSharp.Tests.Internals/Helpers/DebugDispatchDataLoader.cs @@ -0,0 +1,56 @@ +using System; +using System.Diagnostics.Contracts; +using System.Runtime.CompilerServices; +using ComputeSharp.__Internals; + +#pragma warning disable CS0618 + +namespace ComputeSharp.Tests.Internals.Helpers +{ + /// + /// A debug data loader for generic shaders. + /// + internal readonly unsafe struct DebugDispatchDataLoader : IDispatchDataLoader + { + /// + /// The backing, mutable data for and . + /// + private readonly StrongBox<(uint[] Values, ulong[] Resources)> data; + + /// + /// Gets the captured values. + /// + public uint[] Values => this.data.Value.Values; + + /// + /// Gets the captured resources. + /// + public ulong[] Resources => this.data.Value.Resources; + + /// + /// Creates a new instance. + /// + /// A new instance to use. + [Pure] + public static DebugDispatchDataLoader Create() + { + DebugDispatchDataLoader @this = default; + + Unsafe.AsRef(in @this.data) = new StrongBox<(uint[], ulong[])>((Array.Empty(), Array.Empty())); + + return @this; + } + + /// + public void LoadCapturedValues(ReadOnlySpan data) + { + this.data.Value.Values = data.ToArray(); + } + + /// + public void LoadCapturedResources(ReadOnlySpan data) + { + this.data.Value.Resources = data.ToArray(); + } + } +} \ No newline at end of file diff --git a/tests/ComputeSharp.Tests.Internals/ShaderDataLoaderTests.cs b/tests/ComputeSharp.Tests.Internals/ShaderDataLoaderTests.cs index f6b11808..830e5ba9 100644 --- a/tests/ComputeSharp.Tests.Internals/ShaderDataLoaderTests.cs +++ b/tests/ComputeSharp.Tests.Internals/ShaderDataLoaderTests.cs @@ -1,4 +1,4 @@ -using ComputeSharp.__Internals; +using ComputeSharp.Tests.Internals.Helpers; using Microsoft.VisualStudio.TestTools.UnitTesting; #pragma warning disable CS0618 @@ -22,16 +22,19 @@ namespace ComputeSharp.Tests.Internals [TestMethod] public unsafe void CapturedResource() { - ulong* p0 = stackalloc ulong[128]; - byte* p1 = stackalloc byte[256]; - using ReadWriteBuffer buffer = Gpu.Default.AllocateReadWriteBuffer(16); - int size = DispatchDataLoader.LoadDispatchData(Gpu.Default, new CapturedResourceShader(buffer), ref *p0, ref *p1); + DebugDispatchDataLoader dataLoader = DebugDispatchDataLoader.Create(); - Assert.AreEqual(12, size); + new CapturedResourceShader(buffer).LoadDispatchData(in dataLoader, Gpu.Default, 111, 222, 333); - Assert.AreEqual(p0[0], buffer.D3D12GpuDescriptorHandle.ptr); + Assert.AreEqual(3, dataLoader.Values.Length); + Assert.AreEqual(1, dataLoader.Resources.Length); + + Assert.AreEqual(111, (int)dataLoader.Values[0]); + Assert.AreEqual(222, (int)dataLoader.Values[1]); + Assert.AreEqual(333, (int)dataLoader.Values[2]); + Assert.AreEqual(buffer.D3D12GpuDescriptorHandle.ptr, dataLoader.Resources[0]); } [AutoConstructor] @@ -51,22 +54,24 @@ namespace ComputeSharp.Tests.Internals [TestMethod] public unsafe void LoadMultipleResourcesAndPrimitives() { - ulong* p0 = stackalloc ulong[128]; - byte* p1 = stackalloc byte[256]; - using ReadWriteBuffer buffer0 = Gpu.Default.AllocateReadWriteBuffer(16); using ReadWriteBuffer buffer1 = Gpu.Default.AllocateReadWriteBuffer(16); - int size = DispatchDataLoader.LoadDispatchData(Gpu.Default, new MultipleResourcesAndPrimitivesShader(buffer0, buffer1, 1, 22, 77), ref *p0, ref *p1); + DebugDispatchDataLoader dataLoader = DebugDispatchDataLoader.Create(); - Assert.AreEqual(24, size); + new MultipleResourcesAndPrimitivesShader(buffer0, buffer1, 1, 22, 77).LoadDispatchData(in dataLoader, Gpu.Default, 111, 222, 333); - Assert.AreEqual(p0[0], buffer0.D3D12GpuDescriptorHandle.ptr); - Assert.AreEqual(p0[1], buffer1.D3D12GpuDescriptorHandle.ptr); + Assert.AreEqual(6, dataLoader.Values.Length); + Assert.AreEqual(2, dataLoader.Resources.Length); - Assert.AreEqual(1, *(int*)&p1[12]); - Assert.AreEqual(22, *(int*)&p1[16]); - Assert.AreEqual(77, *(int*)&p1[20]); + Assert.AreEqual(111, (int)dataLoader.Values[0]); + Assert.AreEqual(222, (int)dataLoader.Values[1]); + Assert.AreEqual(333, (int)dataLoader.Values[2]); + Assert.AreEqual(1, (int)dataLoader.Values[3]); + Assert.AreEqual(22, (int)dataLoader.Values[4]); + Assert.AreEqual(77, (int)dataLoader.Values[5]); + Assert.AreEqual(buffer0.D3D12GpuDescriptorHandle.ptr, dataLoader.Resources[0]); + Assert.AreEqual(buffer1.D3D12GpuDescriptorHandle.ptr, dataLoader.Resources[1]); } [AutoConstructor] @@ -88,28 +93,36 @@ namespace ComputeSharp.Tests.Internals [TestMethod] public unsafe void LoadScalarAndVectorTypes() { - ulong* p0 = stackalloc ulong[128]; - byte* p1 = stackalloc byte[256]; - using ReadWriteBuffer buffer = Gpu.Default.AllocateReadWriteBuffer(16); + DebugDispatchDataLoader dataLoader = DebugDispatchDataLoader.Create(); ScalarAndVectorTypesShader shader = new(buffer, new(55, 44, 888), 22, 77, new(3.14, 6.28), 42, 9999); - int size = DispatchDataLoader.LoadDispatchData(Gpu.Default, in shader, ref *p0, ref *p1); + shader.LoadDispatchData(in dataLoader, Gpu.Default, 111, 222, 333); - Assert.AreEqual(72, size); + Assert.AreEqual(18, dataLoader.Values.Length); + Assert.AreEqual(1, dataLoader.Resources.Length); - Assert.AreEqual(p0[0], buffer.D3D12GpuDescriptorHandle.ptr); + Assert.AreEqual(111, (int)dataLoader.Values[0]); + Assert.AreEqual(222, (int)dataLoader.Values[1]); + Assert.AreEqual(333, (int)dataLoader.Values[2]); - Assert.AreEqual(55, *(float*)&p1[16]); - Assert.AreEqual(44, *(float*)&p1[20]); - Assert.AreEqual(888, *(float*)&p1[24]); - Assert.AreEqual(22, *(int*)&p1[28]); - Assert.AreEqual(77, *(int*)&p1[32]); - Assert.AreEqual(3.14, *(double*)&p1[48]); - Assert.AreEqual(6.28, *(double*)&p1[56]); - Assert.AreEqual(42, *(int*)&p1[64]); - Assert.AreEqual(9999, *(int*)&p1[68]); + Assert.AreEqual(buffer.D3D12GpuDescriptorHandle.ptr, dataLoader.Resources[0]); + + fixed (void* p0 = &dataLoader.Values[0]) + { + byte* p1 = (byte*)p0; + + Assert.AreEqual(55, *(float*)&p1[16]); + Assert.AreEqual(44, *(float*)&p1[20]); + Assert.AreEqual(888, *(float*)&p1[24]); + Assert.AreEqual(22, *(int*)&p1[28]); + Assert.AreEqual(77, *(int*)&p1[32]); + Assert.AreEqual(3.14, *(double*)&p1[48]); + Assert.AreEqual(6.28, *(double*)&p1[56]); + Assert.AreEqual(42, *(int*)&p1[64]); + Assert.AreEqual(9999, *(int*)&p1[68]); + } } [AutoConstructor] @@ -133,11 +146,9 @@ namespace ComputeSharp.Tests.Internals [TestMethod] public unsafe void LoadScalarVectorAndMatrixTypes() { - ulong* p0 = stackalloc ulong[128]; - byte* p1 = stackalloc byte[256]; - using ReadWriteBuffer buffer = Gpu.Default.AllocateReadWriteBuffer(16); + DebugDispatchDataLoader dataLoader = DebugDispatchDataLoader.Create(); ScalarVectorAndMatrixTypesShader shader = new( buffer, f2x3: new(55, 44, 888, 111, 222, 333), @@ -149,32 +160,42 @@ namespace ComputeSharp.Tests.Internals i2x2: new(11, 22, 33, 44), d: 9999); - int size = DispatchDataLoader.LoadDispatchData(Gpu.Default, in shader, ref *p0, ref *p1); + shader.LoadDispatchData(in dataLoader, Gpu.Default, 111, 222, 333); - Assert.AreEqual(124, size); + Assert.AreEqual(31, dataLoader.Values.Length); + Assert.AreEqual(1, dataLoader.Resources.Length); - Assert.AreEqual(p0[0], buffer.D3D12GpuDescriptorHandle.ptr); + Assert.AreEqual(111, (int)dataLoader.Values[0]); + Assert.AreEqual(222, (int)dataLoader.Values[1]); + Assert.AreEqual(333, (int)dataLoader.Values[2]); - Assert.AreEqual(55, *(float*)&p1[16]); - Assert.AreEqual(44, *(float*)&p1[20]); - Assert.AreEqual(888, *(float*)&p1[24]); - Assert.AreEqual(111, *(float*)&p1[32]); - Assert.AreEqual(222, *(float*)&p1[36]); - Assert.AreEqual(333, *(float*)&p1[40]); - Assert.AreEqual(22, *(int*)&p1[44]); - Assert.AreEqual(1, *(int*)&p1[48]); - Assert.AreEqual(2, *(int*)&p1[52]); - Assert.AreEqual(3, *(int*)&p1[56]); - Assert.AreEqual(3.14, *(double*)&p1[64]); - Assert.AreEqual(6.28, *(double*)&p1[72]); - Assert.AreEqual(42, *(int*)&p1[80]); - Assert.AreEqual(111, *(int*)&p1[84]); - Assert.AreEqual(222, *(int*)&p1[88]); - Assert.AreEqual(11, *(int*)&p1[96]); - Assert.AreEqual(22, *(int*)&p1[100]); - Assert.AreEqual(33, *(int*)&p1[112]); - Assert.AreEqual(44, *(int*)&p1[116]); - Assert.AreEqual(9999, *(int*)&p1[120]); + Assert.AreEqual(buffer.D3D12GpuDescriptorHandle.ptr, dataLoader.Resources[0]); + + fixed (void* p0 = &dataLoader.Values[0]) + { + byte* p1 = (byte*)p0; + + Assert.AreEqual(55, *(float*)&p1[16]); + Assert.AreEqual(44, *(float*)&p1[20]); + Assert.AreEqual(888, *(float*)&p1[24]); + Assert.AreEqual(111, *(float*)&p1[32]); + Assert.AreEqual(222, *(float*)&p1[36]); + Assert.AreEqual(333, *(float*)&p1[40]); + Assert.AreEqual(22, *(int*)&p1[44]); + Assert.AreEqual(1, *(int*)&p1[48]); + Assert.AreEqual(2, *(int*)&p1[52]); + Assert.AreEqual(3, *(int*)&p1[56]); + Assert.AreEqual(3.14, *(double*)&p1[64]); + Assert.AreEqual(6.28, *(double*)&p1[72]); + Assert.AreEqual(42, *(int*)&p1[80]); + Assert.AreEqual(111, *(int*)&p1[84]); + Assert.AreEqual(222, *(int*)&p1[88]); + Assert.AreEqual(11, *(int*)&p1[96]); + Assert.AreEqual(22, *(int*)&p1[100]); + Assert.AreEqual(33, *(int*)&p1[112]); + Assert.AreEqual(44, *(int*)&p1[116]); + Assert.AreEqual(9999, *(int*)&p1[120]); + } } [AutoConstructor] @@ -204,11 +225,9 @@ namespace ComputeSharp.Tests.Internals [TestMethod] public unsafe void LoadFlatCustomTypeShader() { - ulong* p0 = stackalloc ulong[128]; - byte* p1 = stackalloc byte[256]; - using ReadWriteBuffer buffer = Gpu.Default.AllocateReadWriteBuffer(16); + DebugDispatchDataLoader dataLoader = DebugDispatchDataLoader.Create(); FlatCustomTypeShader shader = new( buffer, new SimpleTypes( @@ -221,32 +240,42 @@ namespace ComputeSharp.Tests.Internals i2x2: new(11, 22, 33, 44), d: 9999)); - int size = DispatchDataLoader.LoadDispatchData(Gpu.Default, in shader, ref *p0, ref *p1); + shader.LoadDispatchData(in dataLoader, Gpu.Default, 111, 222, 333); - Assert.AreEqual(124, size); + Assert.AreEqual(31, dataLoader.Values.Length); + Assert.AreEqual(1, dataLoader.Resources.Length); - Assert.AreEqual(p0[0], buffer.D3D12GpuDescriptorHandle.ptr); + Assert.AreEqual(111, (int)dataLoader.Values[0]); + Assert.AreEqual(222, (int)dataLoader.Values[1]); + Assert.AreEqual(333, (int)dataLoader.Values[2]); - Assert.AreEqual(55, *(float*)&p1[16]); - Assert.AreEqual(44, *(float*)&p1[20]); - Assert.AreEqual(888, *(float*)&p1[24]); - Assert.AreEqual(111, *(float*)&p1[32]); - Assert.AreEqual(222, *(float*)&p1[36]); - Assert.AreEqual(333, *(float*)&p1[40]); - Assert.AreEqual(22, *(int*)&p1[44]); - Assert.AreEqual(1, *(int*)&p1[48]); - Assert.AreEqual(2, *(int*)&p1[52]); - Assert.AreEqual(3, *(int*)&p1[56]); - Assert.AreEqual(3.14, *(double*)&p1[64]); - Assert.AreEqual(6.28, *(double*)&p1[72]); - Assert.AreEqual(42, *(int*)&p1[80]); - Assert.AreEqual(111, *(int*)&p1[84]); - Assert.AreEqual(222, *(int*)&p1[88]); - Assert.AreEqual(11, *(int*)&p1[96]); - Assert.AreEqual(22, *(int*)&p1[100]); - Assert.AreEqual(33, *(int*)&p1[112]); - Assert.AreEqual(44, *(int*)&p1[116]); - Assert.AreEqual(9999, *(int*)&p1[120]); + Assert.AreEqual(buffer.D3D12GpuDescriptorHandle.ptr, dataLoader.Resources[0]); + + fixed (void* p0 = &dataLoader.Values[0]) + { + byte* p1 = (byte*)p0; + + Assert.AreEqual(55, *(float*)&p1[16]); + Assert.AreEqual(44, *(float*)&p1[20]); + Assert.AreEqual(888, *(float*)&p1[24]); + Assert.AreEqual(111, *(float*)&p1[32]); + Assert.AreEqual(222, *(float*)&p1[36]); + Assert.AreEqual(333, *(float*)&p1[40]); + Assert.AreEqual(22, *(int*)&p1[44]); + Assert.AreEqual(1, *(int*)&p1[48]); + Assert.AreEqual(2, *(int*)&p1[52]); + Assert.AreEqual(3, *(int*)&p1[56]); + Assert.AreEqual(3.14, *(double*)&p1[64]); + Assert.AreEqual(6.28, *(double*)&p1[72]); + Assert.AreEqual(42, *(int*)&p1[80]); + Assert.AreEqual(111, *(int*)&p1[84]); + Assert.AreEqual(222, *(int*)&p1[88]); + Assert.AreEqual(11, *(int*)&p1[96]); + Assert.AreEqual(22, *(int*)&p1[100]); + Assert.AreEqual(33, *(int*)&p1[112]); + Assert.AreEqual(44, *(int*)&p1[116]); + Assert.AreEqual(9999, *(int*)&p1[120]); + } } [AutoConstructor] @@ -288,11 +317,9 @@ namespace ComputeSharp.Tests.Internals [TestMethod] public unsafe void LoadNestedCustomTypes() { - ulong* p0 = stackalloc ulong[128]; - byte* p1 = stackalloc byte[256]; - using ReadWriteBuffer buffer = Gpu.Default.AllocateReadWriteBuffer(16); + DebugDispatchDataLoader dataLoader = DebugDispatchDataLoader.Create(); NestedCustomTypesShader shader = new( buffer, new CustomType1( @@ -317,45 +344,55 @@ namespace ComputeSharp.Tests.Internals a: 888888, b: new(333.3f, 444.4f)))); - int size = DispatchDataLoader.LoadDispatchData(Gpu.Default, in shader, ref *p0, ref *p1); + shader.LoadDispatchData(in dataLoader, Gpu.Default, 111, 222, 333); - Assert.AreEqual(188, size); + Assert.AreEqual(47, dataLoader.Values.Length); + Assert.AreEqual(1, dataLoader.Resources.Length); - Assert.AreEqual(p0[0], buffer.D3D12GpuDescriptorHandle.ptr); + Assert.AreEqual(111, (int)dataLoader.Values[0]); + Assert.AreEqual(222, (int)dataLoader.Values[1]); + Assert.AreEqual(333, (int)dataLoader.Values[2]); - Assert.AreEqual(3.14f, *(float*)&p1[16]); - Assert.AreEqual(6.28f, *(float*)&p1[20]); - Assert.AreEqual(123.4f, *(float*)&p1[24]); - Assert.AreEqual(55, *(float*)&p1[32]); - Assert.AreEqual(44, *(float*)&p1[36]); - Assert.AreEqual(888, *(float*)&p1[40]); - Assert.AreEqual(111, *(float*)&p1[48]); - Assert.AreEqual(222, *(float*)&p1[52]); - Assert.AreEqual(333, *(float*)&p1[56]); - Assert.AreEqual(22, *(int*)&p1[60]); - Assert.AreEqual(1, *(int*)&p1[64]); - Assert.AreEqual(2, *(int*)&p1[68]); - Assert.AreEqual(3, *(int*)&p1[72]); - Assert.AreEqual(3.14, *(double*)&p1[80]); - Assert.AreEqual(6.28, *(double*)&p1[88]); - Assert.AreEqual(42, *(int*)&p1[96]); - Assert.AreEqual(111, *(int*)&p1[100]); - Assert.AreEqual(222, *(int*)&p1[104]); - Assert.AreEqual(11, *(int*)&p1[112]); - Assert.AreEqual(22, *(int*)&p1[116]); - Assert.AreEqual(33, *(int*)&p1[128]); - Assert.AreEqual(44, *(int*)&p1[132]); - Assert.AreEqual(9999, *(int*)&p1[136]); - Assert.AreEqual(42, *(int*)&p1[140]); - Assert.AreEqual(1234567, *(int*)&p1[144]); - Assert.AreEqual(44.4f, *(float*)&p1[148]); - Assert.AreEqual(55.5f, *(float*)&p1[152]); - Assert.AreEqual(7654321, *(int*)&p1[160]); - Assert.AreEqual(111.1f, *(float*)&p1[164]); - Assert.AreEqual(222.2f, *(float*)&p1[168]); - Assert.AreEqual(888888, *(int*)&p1[176]); - Assert.AreEqual(333.3f, *(float*)&p1[180]); - Assert.AreEqual(444.4f, *(float*)&p1[184]); + Assert.AreEqual(buffer.D3D12GpuDescriptorHandle.ptr, dataLoader.Resources[0]); + + fixed (void* p0 = &dataLoader.Values[0]) + { + byte* p1 = (byte*)p0; + + Assert.AreEqual(3.14f, *(float*)&p1[16]); + Assert.AreEqual(6.28f, *(float*)&p1[20]); + Assert.AreEqual(123.4f, *(float*)&p1[24]); + Assert.AreEqual(55, *(float*)&p1[32]); + Assert.AreEqual(44, *(float*)&p1[36]); + Assert.AreEqual(888, *(float*)&p1[40]); + Assert.AreEqual(111, *(float*)&p1[48]); + Assert.AreEqual(222, *(float*)&p1[52]); + Assert.AreEqual(333, *(float*)&p1[56]); + Assert.AreEqual(22, *(int*)&p1[60]); + Assert.AreEqual(1, *(int*)&p1[64]); + Assert.AreEqual(2, *(int*)&p1[68]); + Assert.AreEqual(3, *(int*)&p1[72]); + Assert.AreEqual(3.14, *(double*)&p1[80]); + Assert.AreEqual(6.28, *(double*)&p1[88]); + Assert.AreEqual(42, *(int*)&p1[96]); + Assert.AreEqual(111, *(int*)&p1[100]); + Assert.AreEqual(222, *(int*)&p1[104]); + Assert.AreEqual(11, *(int*)&p1[112]); + Assert.AreEqual(22, *(int*)&p1[116]); + Assert.AreEqual(33, *(int*)&p1[128]); + Assert.AreEqual(44, *(int*)&p1[132]); + Assert.AreEqual(9999, *(int*)&p1[136]); + Assert.AreEqual(42, *(int*)&p1[140]); + Assert.AreEqual(1234567, *(int*)&p1[144]); + Assert.AreEqual(44.4f, *(float*)&p1[148]); + Assert.AreEqual(55.5f, *(float*)&p1[152]); + Assert.AreEqual(7654321, *(int*)&p1[160]); + Assert.AreEqual(111.1f, *(float*)&p1[164]); + Assert.AreEqual(222.2f, *(float*)&p1[168]); + Assert.AreEqual(888888, *(int*)&p1[176]); + Assert.AreEqual(333.3f, *(float*)&p1[180]); + Assert.AreEqual(444.4f, *(float*)&p1[184]); + } } } } diff --git a/tests/ComputeSharp.Tests.Internals/ShaderHashCodeTests.cs b/tests/ComputeSharp.Tests.Internals/ShaderHashCodeTests.cs index 241ce61b..5d8b7d59 100644 --- a/tests/ComputeSharp.Tests.Internals/ShaderHashCodeTests.cs +++ b/tests/ComputeSharp.Tests.Internals/ShaderHashCodeTests.cs @@ -1,14 +1,15 @@ using System; -using ComputeSharp.Shaders.Translation; using Microsoft.VisualStudio.TestTools.UnitTesting; +#pragma warning disable CS0618 + namespace ComputeSharp.Tests.Internals { [TestClass] [TestCategory("ShaderHashCode")] - public class ShaderHashCodeTests + public partial class ShaderHashCodeTests { - public struct Shader1 : IComputeShader + public partial struct Shader1 : IComputeShader { public float A; public ReadWriteBuffer B; @@ -28,19 +29,19 @@ namespace ComputeSharp.Tests.Internals Shader1 shader1 = new() { A = value, B = buffer }; int - hash1 = ShaderHashCodeProvider.GetHashCode(shader1), - hash2 = ShaderHashCodeProvider.GetHashCode(shader1); + hash1 = shader1.GetDispatchId(), + hash2 = shader1.GetDispatchId(); Assert.IsTrue(hash1 == hash2); Shader1 shader2 = new() { A = value, B = buffer }; - int hash3 = ShaderHashCodeProvider.GetHashCode(shader2); + int hash3 = shader2.GetDispatchId(); Assert.IsTrue(hash1 == hash3); } - public struct Shader2 : IComputeShader + public partial struct Shader2 : IComputeShader { public float A; public ReadWriteBuffer B; @@ -62,8 +63,8 @@ namespace ComputeSharp.Tests.Internals Shader2 shader1 = new() { A = 1, B = buffer, F = f }; int - hash1 = ShaderHashCodeProvider.GetHashCode(shader1), - hash2 = ShaderHashCodeProvider.GetHashCode(shader1); + hash1 = shader1.GetDispatchId(), + hash2 = shader1.GetDispatchId(); Assert.IsTrue(hash1 == hash2); @@ -71,7 +72,7 @@ namespace ComputeSharp.Tests.Internals Shader2 shader2 = new() { A = 1, B = buffer, F = f }; - int hash3 = ShaderHashCodeProvider.GetHashCode(shader2); + int hash3 = shader2.GetDispatchId(); Assert.IsFalse(hash1 == hash3); } diff --git a/tests/ComputeSharp.Tests/ShadersTests.cs b/tests/ComputeSharp.Tests/ShadersTests.cs index f8bad618..8f9862ee 100644 --- a/tests/ComputeSharp.Tests/ShadersTests.cs +++ b/tests/ComputeSharp.Tests/ShadersTests.cs @@ -2,6 +2,7 @@ using System; using System.IO; using System.Reflection; using System.Runtime.InteropServices; +using ComputeSharp.__Internals; using ComputeSharp.SwapChain.Shaders; using ComputeSharp.Tests.Attributes; using ComputeSharp.Tests.Extensions; @@ -10,6 +11,8 @@ using SixLabors.ImageSharp; using SixLabors.ImageSharp.Formats.Png; using ImageSharpRgba32 = SixLabors.ImageSharp.PixelFormats.Rgba32; +#pragma warning disable CS0618 + namespace ComputeSharp.Tests { [TestClass] @@ -137,7 +140,7 @@ namespace ComputeSharp.Tests if (shaderType.IsAssignableTo(typeof(IComputeShader))) { static void RunComputeShader(ReadWriteTexture2D texture) - where T : struct, IComputeShader + where T : struct, IComputeShader, IShader { texture.GraphicsDevice.For(texture.Width, texture.Height, (T)Activator.CreateInstance(typeof(T), texture, 0f)!); } @@ -149,7 +152,7 @@ namespace ComputeSharp.Tests else { static void RunPixelShader(ReadWriteTexture2D texture) - where T : struct, IPixelShader + where T : struct, IPixelShader, IShader { texture.GraphicsDevice.ForEach(texture, (T)Activator.CreateInstance(typeof(T), 0f)!); } @@ -188,8 +191,8 @@ namespace ComputeSharp.Tests Func, TCompute> computeFactory, Func, TPixel> pixelFactory, float delta) - where TCompute : struct, IComputeShader - where TPixel : struct, IPixelShader + where TCompute : struct, IComputeShader, IShader + where TPixel : struct, IPixelShader, IShader { _ = device.Get();