From 512efa074bde66f16fc1bac56cd12f968be7c941 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Wed, 29 Nov 2023 22:19:34 +0100 Subject: [PATCH] Add unit tests for shader with partial declarations --- .../ComputeSharp.Tests/ShaderCompilerTests.cs | 87 +++++++++++++++++++ 1 file changed, 87 insertions(+) diff --git a/tests/ComputeSharp.Tests/ShaderCompilerTests.cs b/tests/ComputeSharp.Tests/ShaderCompilerTests.cs index 39b68c80..d6dcff9b 100644 --- a/tests/ComputeSharp.Tests/ShaderCompilerTests.cs +++ b/tests/ComputeSharp.Tests/ShaderCompilerTests.cs @@ -787,6 +787,93 @@ namespace ComputeSharp.Tests { } } + + [TestMethod] + public void ShaderWithPartialDeclarations_IsCombinedCorrectly() + { + ShaderInfo info = ReflectionServices.GetShaderInfo(); + + Assert.AreEqual( + """ + // ================================================ + // AUTO GENERATED + // ================================================ + // This shader was created by ComputeSharp. + // See: https://github.com/Sergio0694/ComputeSharp. + + #define __GroupSize__get_X 64 + #define __GroupSize__get_Y 1 + #define __GroupSize__get_Z 1 + #define __ComputeSharp_Tests_ShaderCompilerTests_ShaderWithPartialDeclarations__a 2 + + static const int b = 4; + + cbuffer _ : register(b0) + { + uint __x; + uint __y; + uint __z; + } + + RWStructuredBuffer __reserved__buffer : register(u0); + + static int Sum(int x, int y); + + static int Sum(int x, int y) + { + return x + y; + } + + [NumThreads(__GroupSize__get_X, __GroupSize__get_Y, __GroupSize__get_Z)] + void Execute(uint3 ThreadIds : SV_DispatchThreadID) + { + if (ThreadIds.x < __x && ThreadIds.y < __y && ThreadIds.z < __z) + { + __reserved__buffer[ThreadIds.x] = Sum(__ComputeSharp_Tests_ShaderCompilerTests_ShaderWithPartialDeclarations__a, b); + } + } + """, + info.HlslSource); + } + + [AutoConstructor] + [ThreadGroupSize(DefaultThreadGroupSizes.X)] + [GeneratedComputeShaderDescriptor] + internal readonly partial struct ShaderWithPartialDeclarations : IComputeShader + { + } + + partial struct ShaderWithPartialDeclarations + { + public readonly ReadWriteBuffer buffer; + } + + partial struct ShaderWithPartialDeclarations + { + /// + public void Execute() + { + buffer[ThreadIds.X] = Sum(a, b); + } + } + + partial struct ShaderWithPartialDeclarations + { + private const int a = 2; + } + + partial struct ShaderWithPartialDeclarations + { + private static readonly int b = 4; + } + + partial struct ShaderWithPartialDeclarations + { + private static int Sum(int x, int y) + { + return x + y; + } + } } }