diff --git a/tests/ComputeSharp.Tests.Internals/ShaderDataLoaderTests.cs b/tests/ComputeSharp.Tests.Internals/ShaderDataLoaderTests.cs index fb729401..0d2a7f45 100644 --- a/tests/ComputeSharp.Tests.Internals/ShaderDataLoaderTests.cs +++ b/tests/ComputeSharp.Tests.Internals/ShaderDataLoaderTests.cs @@ -10,7 +10,7 @@ namespace ComputeSharp.Tests.Internals public partial class ShaderDataLoaderTests { [AutoConstructor] - public partial struct Shader1 : IComputeShader + public partial struct CapturedResourceShader : IComputeShader { public ReadWriteBuffer _; @@ -20,14 +20,14 @@ namespace ComputeSharp.Tests.Internals } [TestMethod] - public unsafe void LoadShader1() + 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 Shader1(buffer), ref *p0, ref *p1); + int size = DispatchDataLoader.LoadDispatchData(Gpu.Default, new CapturedResourceShader(buffer), ref *p0, ref *p1); Assert.AreEqual(12, size); @@ -35,7 +35,7 @@ namespace ComputeSharp.Tests.Internals } [AutoConstructor] - public partial struct Shader2 : IComputeShader + public partial struct MultipleResourcesAndPrimitivesShader : IComputeShader { public ReadWriteBuffer _0; public ReadWriteBuffer _1; @@ -49,7 +49,7 @@ namespace ComputeSharp.Tests.Internals } [TestMethod] - public unsafe void LoadShader2() + public unsafe void LoadMultipleResourcesAndPrimitives() { ulong* p0 = stackalloc ulong[128]; byte* p1 = stackalloc byte[256]; @@ -57,7 +57,7 @@ namespace ComputeSharp.Tests.Internals using ReadWriteBuffer buffer0 = Gpu.Default.AllocateReadWriteBuffer(16); using ReadWriteBuffer buffer1 = Gpu.Default.AllocateReadWriteBuffer(16); - int size = DispatchDataLoader.LoadDispatchData(Gpu.Default, new Shader2(buffer0, buffer1, 1, 22, 77), ref *p0, ref *p1); + int size = DispatchDataLoader.LoadDispatchData(Gpu.Default, new MultipleResourcesAndPrimitivesShader(buffer0, buffer1, 1, 22, 77), ref *p0, ref *p1); Assert.AreEqual(24, size); @@ -70,7 +70,7 @@ namespace ComputeSharp.Tests.Internals } [AutoConstructor] - public partial struct Shader3 : IComputeShader + public partial struct ScalarAndVectorTypesShader : IComputeShader { public ReadWriteBuffer _; public Float3 f3; @@ -86,14 +86,14 @@ namespace ComputeSharp.Tests.Internals } [TestMethod] - public unsafe void LoadShader3() + public unsafe void LoadScalarAndVectorTypes() { ulong* p0 = stackalloc ulong[128]; byte* p1 = stackalloc byte[256]; using ReadWriteBuffer buffer = Gpu.Default.AllocateReadWriteBuffer(16); - Shader3 shader = new(buffer, new(55, 44, 888), 22, 77, new(3.14, 6.28), 42, 9999); + 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); @@ -113,7 +113,7 @@ namespace ComputeSharp.Tests.Internals } [AutoConstructor] - public partial struct Shader4 : IComputeShader + public partial struct ScalarVectorAndMatrixTypesShader : IComputeShader { public ReadWriteBuffer _; public Float2x3 f2x3; @@ -131,14 +131,14 @@ namespace ComputeSharp.Tests.Internals } [TestMethod] - public unsafe void LoadShader4() + public unsafe void LoadScalarVectorAndMatrixTypes() { ulong* p0 = stackalloc ulong[128]; byte* p1 = stackalloc byte[256]; using ReadWriteBuffer buffer = Gpu.Default.AllocateReadWriteBuffer(16); - Shader4 shader = new( + ScalarVectorAndMatrixTypesShader shader = new( buffer, f2x3: new(55, 44, 888, 111, 222, 333), a: 22, @@ -176,5 +176,186 @@ namespace ComputeSharp.Tests.Internals Assert.AreEqual(44, *(int*)&p1[116]); Assert.AreEqual(9999, *(int*)&p1[120]); } + + [AutoConstructor] + public partial struct FlatCustomTypeShader : IComputeShader + { + public ReadWriteBuffer _0; + public SimpleTypes a; + + public void Execute() + { + } + } + + [TestMethod] + public unsafe void LoadFlatCustomTypeShader() + { + ulong* p0 = stackalloc ulong[128]; + byte* p1 = stackalloc byte[256]; + + using ReadWriteBuffer buffer = Gpu.Default.AllocateReadWriteBuffer(16); + + FlatCustomTypeShader shader = new( + buffer, + new SimpleTypes( + f2x3: new(55, 44, 888, 111, 222, 333), + a: 22, + i1x3: new(1, 2, 3), + d2: new(3.14, 6.28), + c: 42, + i1x2: new(111, 222), + i2x2: new(11, 22, 33, 44), + d: 9999)); + + int size = DispatchDataLoader.LoadDispatchData(Gpu.Default, in shader, ref *p0, ref *p1); + + Assert.AreEqual(124, size); + + Assert.AreEqual(p0[0], buffer.D3D12GpuDescriptorHandle.ptr); + + 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] + public partial struct NestedCustomTypesShader : IComputeShader + { + public ReadWriteBuffer _0; + public CustomType1 a; + + public void Execute() + { + } + } + + [TestMethod] + public unsafe void LoadNestedCustomTypes() + { + ulong* p0 = stackalloc ulong[128]; + byte* p1 = stackalloc byte[256]; + + using ReadWriteBuffer buffer = Gpu.Default.AllocateReadWriteBuffer(16); + + NestedCustomTypesShader shader = new( + buffer, + new CustomType1( + a: new(3.14f, 6.28f, 123.4f), + b: new SimpleTypes( + f2x3: new(55, 44, 888, 111, 222, 333), + a: 22, + i1x3: new(1, 2, 3), + d2: new(3.14, 6.28), + c: 42, + i1x2: new(111, 222), + i2x2: new(11, 22, 33, 44), + d: 9999), + c: 42, + d: new CustomType2( + a: 1234567, + b: new(44.4f, 55.5f), + c: new CustomType3( + a: 7654321, + b: new (111.1f, 222.2f))), + e: new CustomType3( + a: 888888, + b: new(333.3f, 444.4f)))); + + int size = DispatchDataLoader.LoadDispatchData(Gpu.Default, in shader, ref *p0, ref *p1); + + Assert.AreEqual(188, size); + + Assert.AreEqual(p0[0], buffer.D3D12GpuDescriptorHandle.ptr); + + 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]); + } + } + + [AutoConstructor] + public partial struct SimpleTypes + { + public Float2x3 f2x3; + public int a; + public Int1x3 i1x3; + public Double2 d2; + public int c; + public Int1x2 i1x2; + public Int2x2 i2x2; + public int d; + } + + [AutoConstructor] + public partial struct CustomType1 + { + public Float3 a; + public SimpleTypes b; + public int c; + public CustomType2 d; + public CustomType3 e; + } + + [AutoConstructor] + public partial struct CustomType2 + { + public int a; + public Float2 b; + public CustomType3 c; + } + + [AutoConstructor] + public partial struct CustomType3 + { + public int a; + public Float2 b; } }